Reputation: 171
I am working on a application where user can set values via an interface and send ajax request (similar to as rest api).
I would like to know how to send data belong to multiple types in a single request. Example is given below,
var data = {'key1':'Key1Value','key2':'Key2Value'};
//content-type : application/x-www-form-urlencoded
var data = 'This is plain text raw value';
//content-type : text/plain
How should I combine above values to send the ajax request below,
$.ajax({
type: "POST",
url: "ajax/activity.php",
data: {combined data},
timeout: 3000,
async: true,
success: APIClient.baseSuccess,
error: APIClient.baseError
});
Upvotes: 0
Views: 1970
Reputation: 8698
You could use JSON to send all your various types of data:
{
"data": {
"arrayOfValues": {
"key1": "Key1Value",
"key2": "Key2Value"
},
"someString": "This is some tring value that I want to send"
}
}
Assign this object to a variable and send it in your AJAX call. Of course you need to factor in what/how this data will be received server side.
Upvotes: 0
Reputation: 27051
Use the $.extend()
function.
var dataText = 'This is plain text raw value';
$.ajax({
type: "POST",
url: "ajax/activity.php",
data: $.extend({
key1:'Key1Value',key2:'Key2Value'
}, dataText),
timeout: 3000,
async: true,
success: APIClient.baseSuccess,
error: APIClient.baseError
});
Upvotes: 1
Reputation: 633
Maybe send json object with those two as parameters?
$.ajax({
type: "POST",
url: "ajax/activity.php",
data: {
first: data_object,
second: data_string
},
timeout: 3000,
async: true,
success: APIClient.baseSuccess,
error: APIClient.baseError
});
Upvotes: 2
Reputation: 6969
Try like this..
var data = {'key1':'Key1Value','key2':'Key2Value'};
var text= 'This is plain text raw value';
data.text = text; //adds text to data object
Then
In ajax
data: data,
Upvotes: 1