-1

我正在尝试发送 Ajax 请求,但我想包含已在发送到服务器的数据中定义的变量。

我不太确定如何逃避数据部分并在我所说的地方放置一个变量......

$.ajax({                                      
  url: './json/delete.php',                       
  type: 'POST',
  async: false,
  data: { SD_FieldDisplayName : <VARIABLE HERE>, 
          SD_FieldSeq : <VARIABLE HERE>, 
          SD_TableSeq : <VARIABLE HERE>, 
          SD_ViewName : <VARIABLE HERE> }                      
  dataType: 'json',                   
  success: function(result)          
  {
4

4 回答 4

1

您在data对象后忘记了逗号。

$.ajax({
    url: './json/delete.php',
    type: 'POST',
    async: false,
    data: { SD_FieldDisplayName : <VARIABLE HERE>,
            SD_FieldSeq : <VARIABLE HERE>,
            SD_TableSeq : <VARIABLE HERE>,
            SD_ViewName : <VARIABLE HERE> },
                  // You need a comma here ^
    dataType: 'json',
    success: function(result)
    {
    }
});
于 2013-10-02T14:52:08.043 回答
0

您可以只引用以下变量中的value变量key

var someVar = 3;

data: { SD_FieldDisplayName : someVar }
于 2013-10-02T14:45:53.097 回答
0

You're almost there. You need to include in the variables you want to send through your AJAX request. Here's a stripped example. I've used $.post but $.ajax is essentially the same. It takes the data from a form with the class .formclicked (which other code labels with the class .formclicked. It sends through a 'relations' data with $_POST['relations'] and a method flag $_POST['method']. The first is defined by the form, the second is defined y the submit button in the form, that's not sent by serialise. The serialise function converts the form data in #an_id into a form suitable for AJAX.

            // Comments
            jQuery(document).on('submit','#an_id' ,function(){
                $data = jQuery(this).serialize();
                var selector = jQuery(this);
                $method = jQuery(this).find(".formclicked").attr('value'); // Get clicked form
                jQuery.post('your_ajax_form.php', {action:'hook_update',relations:$data, method:$method},
                    function(answer){
                        if(jQuery.isNumeric(answer)){
                            if(answer) {  
                                   // Response Code Based on Condition
                            }
                            else {
                                    // Handle failures
                   jQuery(selector).find(".formclicked").removeClass('.formclicked');

                            } 
                        }

                        else {

                        }

                        return true;
                    }
                );
                return false; // Prevent the page from refreshing
            });
于 2013-10-02T15:05:31.483 回答
-1

请使用这个:

var val1;
var val2;
var val3;
var val4;

$.ajax({                                      
  url: './json/delete.php',                       
  type: 'POST',
  async: false,
  data: { SD_FieldDisplayName : val1 , SD_FieldSeq : val2 , SD_TableSeq : val3 , SD_ViewName : val4 },                     
  dataType: 'json',                   
  success: function(result)          
  {

  }
  });

请在要调用此 ajxa 调用或全局的函数内定义变量

于 2013-10-02T14:55:00.130 回答