Javascript 在ajax请求中传递对象
我需要在ajax请求中传递对象,以便将文件或数据“放入”rest服务。我怎么做?多谢各位 更新 我有以下代码:Javascript 在ajax请求中传递对象,javascript,jquery,ajax,object,Javascript,Jquery,Ajax,Object,我需要在ajax请求中传递对象,以便将文件或数据“放入”rest服务。我怎么做?多谢各位 更新 我有以下代码: var invoice = {}; invoice.POSWorkstationID = "POS7"; invoice.POSClerkID = "admin"; invoice.CustomerName = "Alice in Wonderland Tours"; invoice.IsFreightOverwrite =
var invoice = {};
invoice.POSWorkstationID = "POS7";
invoice.POSClerkID = "admin";
invoice.CustomerName = "Alice in Wonderland Tours";
invoice.IsFreightOverwrite = true;
我应该这样做:
parameter = "{BillToCode:"+invoice.CustomerName+",POSWorkstationID:"+invoice.POSWorkstationID+",POSClerkID:"+invoice.POSClerkID+",IsFreightOverwrite:"+invoice.IsFrieghtOverwrite+"}";
这是:
data: JSON.stringify(parameter),
看看jquerypost 你有几个选择:
$.post("test.php", $("#testform").serialize());
$.post("test.php", { name: "John", time: "2pm" } );
看看jquerypost 你有几个选择:
$.post("test.php", $("#testform").serialize());
$.post("test.php", { name: "John", time: "2pm" } );
通常,您可以使用jquery执行以下操作:
$.ajax(
{
type: "PUT",
dataType: "json",
data:POSTData,
url: 'www.youurlhere.com/path',
complete: function(xhr, statusText)
{
switch(xhr.status)
{
//here handle the response
}
}
});
POSTData是u提供给rest的json格式的数据,您可以通过简单地推送属性将对象转换为json格式,但要遵守json格式语法通常,您可以使用jquery这样做:
$.ajax(
{
type: "PUT",
dataType: "json",
data:POSTData,
url: 'www.youurlhere.com/path',
complete: function(xhr, statusText)
{
switch(xhr.status)
{
//here handle the response
}
}
});
POSTData是u提供给rest的json格式的数据,您可以通过简单地推送属性将对象转换为json格式,但要遵守json格式语法,客户端和服务器端通信的最佳方式是(IMHO)json。 您可以使用此轻量级库将对象序列化为json格式=>
寻找stringify方法。客户端和服务器端通信的最佳方式是(IMHO)JSON。 您可以使用此轻量级库将对象序列化为json格式=> 寻找字符串化方法