如何通过在javascript中传递实体来创建post方法ajax调用?

问题描述:

我需要做一个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 }, 

下面是一个示例,我将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