我需要发出 post ajax 请求,其中我需要将实体类作为参数传递。
例如:
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json
)]
public bool SaveEmployee(Employee employee)
{
//inserting the data to database.
}
这里我的实体类是 Employee,它公开了 2,3 个属性,例如 empId、employeename 和 age。
我需要从 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 },
最佳答案
这是我放在一起的一个示例,它将一个实体发布到 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
。除此之外,这几乎是一个开箱即用的配置。
所以关键是我的有效载荷是一个转换为 JSON 化字符串的 JavaScript 对象字面量。我的服务期望请求格式为 JSON,并且由于我的 Product
类装饰有 DataMember
和 DataContract
属性,它可以反序列化我的有效负载到 Product
类实例。
希望对您有所帮助。如果您还有其他问题,请告诉我,我会相应地更新我的答案。
关于javascript - 如何通过在 javascript 中传递实体来进行 post 方法 ajax 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3501183/