Reputation: 3223
I need to pass objects in ajax request in order to "PUT" files or data to my rest service. How can i do it? Thank you.
i have this code:
var invoice = {};
invoice.POSWorkstationID = "POS7";
invoice.POSClerkID = "admin";
invoice.CustomerName = "Alice in Wonderland Tours";
invoice.IsFreightOverwrite = true;
should i do this:
parameter = "{BillToCode:"+invoice.CustomerName+",POSWorkstationID:"+invoice.POSWorkstationID+",POSClerkID:"+invoice.POSClerkID+",IsFreightOverwrite:"+invoice.IsFrieghtOverwrite+"}";
and this:
data: JSON.stringify(parameter),
Upvotes: 0
Views: 4693
Reputation: 531
The best way to communicate client and server side is (IMHO) JSON. You could serialize your object into json format, with this lightweight library => http://www.json.org/js.html Look for stringify method.
Upvotes: 1
Reputation: 20140
Normally, you can use jquery to do this may be like this:
$.ajax(
{
type: "PUT",
dataType: "json",
data:POSTData,
url: 'www.youurlhere.com/path',
complete: function(xhr, statusText)
{
switch(xhr.status)
{
//here handle the response
}
}
});
POSTData is the data in json format that u supply to the rest, you can turn an object into a json format by simply pushing the attributes but respecting JSON Format syntax
Upvotes: 1
Reputation: 5912
Take a look at jQuery post http://api.jquery.com/jQuery.post/ you have few options there:
$.post("test.php", $("#testform").serialize());
$.post("test.php", { name: "John", time: "2pm" } );
Upvotes: 1