0

Simple question here. I want to send two strings as data in an ajax request. Problem is in this ajax request it only sends the second sting of data and not the first. How would I send both of those in one ajax request?

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

I want to send "credit_uri" and "address". How?

4 Answers 4

5

Objects cannot contain duplicate keys, which in your case is data.

Use an object literal:

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

$.ajax will convert the data to a query string.

Sign up to request clarification or add additional context in comments.

Comments

1

with data you can send multiple data to the server.

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

Its like an object.

1 Comment

Your always Welcome :)
1

You can try this.

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

Comments

1

Try this code :-

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.