0

这里简单的问题。我想在 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);
    }
});

我想发送“credit_uri”和“address”。如何?

4

4 回答 4

5

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

使用对象文字:

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

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

于 2013-09-10T04:45:10.647 回答
1

试试这个代码: -

$.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);
        }
    });
于 2013-09-10T04:58:49.280 回答
1

使用数据,您可以将多个数据发送到服务器。

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

它就像一个物体。

于 2013-09-10T04:47:41.007 回答
1

你可以试试这个。

$.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);
        }
    });
于 2013-09-10T04:52:56.287 回答