如何通过在javascript中传递实体来进行post方法ajax调用?
我需要发出一个post ajax请求,其中我需要将实体类作为参数传递 例如:如何通过在javascript中传递实体来进行post方法ajax调用?,javascript,ajax,wcf,rest,Javascript,Ajax,Wcf,Rest,我需要发出一个post ajax请求,其中我需要将实体类作为参数传递 例如: [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json )] public bool SaveEmployee(Employee employee)
[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>
我的实体是这样的:
[DataContract]
public class Product
{
[DataMember()]
public String Name { get; set; }
[DataMember()]
public String Desc { get; set; }
}
<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>
My web.config的设置如下:
[DataContract]
public class Product
{
[DataMember()]
public String Name { get; set; }
[DataMember()]
public String Desc { get; set; }
}
<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
类用DataMember
和DataContract
属性修饰,它可以将我的有效负载反序列化为Product
类实例
我希望这有帮助。如果您还有其他问题,请告诉我,我会相应地更新我的答案。下面是我收集的一个示例,它将一个实体发布到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>
我的实体是这样的:
[DataContract]
public class Product
{
[DataMember()]
public String Name { get; set; }
[DataMember()]
public String Desc { get; set; }
}
<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>
My web.config的设置如下:
[DataContract]
public class Product
{
[DataMember()]
public String Name { get; set; }
[DataMember()]
public String Desc { get; set; }
}
<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
类用DataMember
和DataContract
属性修饰,它可以将我的有效负载反序列化为Product
类实例
我希望这有帮助。如果您还有其他问题,请告诉我,我会相应地更新我的答案。Hi。。我没有使用wcf。我正在使用我现有的实体。这是一个巨大的应用。因此,我不能更改任何属性。你能告诉我我能为其他正规实体做些什么吗。。我没有使用wcf。我正在使用我现有的实体。这是一个巨大的应用。因此,我不能更改任何属性。你能告诉我我能为其他常规实体做些什么吗。