Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用AJAX发送多个数据_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 使用AJAX发送多个数据

Javascript 使用AJAX发送多个数据,javascript,jquery,ajax,Javascript,Jquery,Ajax,这里有一个简单的问题。我想在ajax请求中发送两个字符串作为数据。问题是在这个ajax请求中,它只发送第二个数据,而不是第一个。如何在一个ajax请求中发送这两个请求 $.ajax({ url: '#{add_cards_path}', type: 'POST', beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#{form_authenticity_token}')}, dataType: "jso

这里有一个简单的问题。我想在ajax请求中发送两个字符串作为数据。问题是在这个ajax请求中,它只发送第二个数据,而不是第一个。如何在一个ajax请求中发送这两个请求

$.ajax({ url: '#{add_cards_path}', 
  type: 'POST',
  beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#{form_authenticity_token}')},
  dataType: "json",

  data: 'credit_uri=' + response.data.uri,  
  data: 'address=' + $('.address').val(),
  success: function(response) {
    window.location.assign(location.protocol + '//' + location.host);
    }
});

我想发送“信用卡uri”和“地址”。如何操作?

对象不能包含重复的键,在您的情况下是
数据

使用对象文字:

data: {credit_uri: response.data.uri, address: $('.address').val() }

$。ajax
将数据转换为查询字符串。

使用数据,您可以向服务器发送多个数据

data: {credit_uri: response.data.uri, address: $('.address').val() }
它就像一个物体。

你可以试试这个

$.ajax({ url: '#{add_cards_path}', 
      type: 'POST',
      beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#    {form_authenticity_token}')},
      dataType: "json",
      data: {"credit_uri:" + response.data.uri + ", "address:" + $('.address').val()} 
      success: function(response) {
        window.location.assign(location.protocol + '//' + location.host);
        }
    });
请尝试以下代码:-

$.ajax({ url: '#{add_cards_path}', 
      type: 'POST',
      beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#{form_authenticity_token}')},
      dataType: "json",

      data: 'credit_uri=' + response.data.uri+'&address='+$('.address').val(),
      success: function(response) {
        window.location.assign(location.protocol + '//' + location.host);
        }
    });

永远欢迎您:)