2010-08-17 38 views
2

我需要做一個post ajax請求,在我需要傳遞實體類作爲參數。如何通過在javascript中傳遞實體來創建post方法ajax調用?

爲如:

[OperationContract] 
    [WebInvoke(Method = "POST", 
      BodyStyle = WebMessageBodyStyle.Wrapped, 
      ResponseFormat = WebMessageFormat.Json 
    )] 
    public bool SaveEmployee(Employee employee) 
    { 
     //inserting the data to database. 
    } 

這裏我的實體類是員工擁有2,3性質暴露出來,說EMPID,employeename和年齡。

這些信息我需要從javascript傳遞。

function SaveEmployee() { 

var employeename='test'; 
var age=23; 
var empid=34534; 
//these data's i need to pass to the ajax method. 
     var am = new Proxy(); 
     am.invoke("SaveEmployee", { **here how can i pass my entity Employee?** }, function(response){ 
      if (response) { 

我可以在JavaScript中做這樣的事嗎?

var employee=new Employee(); 
employee.Name=employeename; 
employee.Age=age; 
employee.EmpId=empid; 
and am.invoke("SaveEmployee", { "Employee":employee }, 

回答

0

下面是一個示例,我將POST實體POST到WCF REST服務(在客戶端使用jQuery)。如果你不使用jQuery,我認爲你仍然會看到如何處理它。

這裏的HTML &的JavaScript:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
</head> 
<body> 
    Name: <input type="text" id="name" /><br /> 
    Desc: <input type="text" id="desc" /><br /> 
    <button id="submit">Send!</button> 

    <script type="text/javascript"> 
     $(function() { 
      $("#submit").click(function (e) { 
       e.preventDefault(); 
       var theData = {}; 
       theData["Name"] = $("#name").val(); 
       theData["Desc"] = $("#desc").val(); 
       $.ajax({ 
        type: "POST", 
        url: "ProdService.svc/product/add", 
        data: JSON.stringify(theData), 
        dataType: "json", 
        contentType: "application/json", 
        success: function (data) { 
         alert(data); 
        }, 
        error: function (e, t, x) { 
         alert(t); 
        } 
       }); 
      }); 
     }); 
    </script> 
</body> 
</html> 

要注意的關鍵事情是,我請求的內容類型設置爲application/json。這對於WCF來說很關鍵,所以它知道發送了什麼。

我的服務被定義爲這樣的:

[OperationContract] 
[WebInvoke(Method="POST", ResponseFormat=WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json, 
    BodyStyle=WebMessageBodyStyle.Bare, UriTemplate="product/add")] 
String AddProduct(Product p) 
{ 
    return "Got " + p.Name; 
} 

我的實體是這樣的:

[DataContract] 
public class Product 
{ 
    [DataMember()] 
    public String Name { get; set; } 
    [DataMember()] 
    public String Desc { get; set; } 
} 

我的web.config設置像這樣:

<system.serviceModel> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior name="WebApplication2.ProdServiceAspNetAjaxBehavior"> 
       <webHttp /> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
     multipleSiteBindingsEnabled="true" /> 
    <services> 
     <service name="WebApplication2.ProdService"> 
      <endpoint address="" behaviorConfiguration="WebApplication2.ProdServiceAspNetAjaxBehavior" 
       binding="webHttpBinding" contract="WebApplication2.ProdService" /> 
     </service> 
    </services> 
</system.serviceModel> 

的在web.config中的關鍵是我的endpointBehavior使用webHttp而不是enableWebScript 。除此之外,這幾乎是一個開箱即用的配置。

所以關鍵是我的負載是一個JavaScript對象文字轉換爲JSON字符串。我的服務期望請求格式爲JSON,並且由於我的Product類使用DataMemberDataContract屬性進行了裝飾,所以它可以將我的負載反序列化爲Product類實例。

我希望這會有所幫助。如果您有其他問題,請告訴我,我會相應地更新我的答案。

+0

嗨..我沒有使用wcf。我正在使用我現有的實體。它是一個巨大的應用。因此我無法更改任何屬性。你能告訴我如何爲其他正規實體做些什麼? – nimi 2010-11-24 09:58:38

相關問題