2012-03-05 135 views
0

我需要在ajax请求中传递对象以便将文件或数据“PUT”到我的休息服务。我该怎么做?谢谢。在ajax请求中传递对象

更新

我有这样的代码:

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), 

回答

1

通常情况下,你可以使用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是JSON格式,美提供给其他的数据,你可以通过简单地按下属性来将对象变成json格式,但是尊重JSON格式语法

1

沟通客户端和服务器端的最佳方式是(恕我直言)JSON。 您可以将您的对象序列化为json格式,使用此轻量级库=> http://www.json.org/js.html 寻找stringify方法。