使用 AJAX 发送多个数据

Sending multiple data with AJAX

本文关键字:数据 AJAX 使用      更新时间:2023-09-26

简单的问题在这里。我想在 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"和"地址"。如何?

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

使用对象文本:

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);
        }
    });