如何使用参数从jquery ajax调用跨域WCF服务 [英] How to call a cross domain WCF service from jquery ajax with parameters
本文介绍了如何使用参数从jquery ajax调用跨域WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I have one Cross Domain WCF. I have already implemented the CORS on the service.
If the method don't have any parameter, then I can call it using JQuery Ajax.
But the problem is, when the method is having any parameter then some error is occurred after the JQuery Ajax call.
$.support.cors = true;
$.ajax({
url: servicePath,
crossDomain: true,
type: 'POST',
async: false,
cache: false,
data: '{"name": "xyz"}',
contentType: "application/json; charset=utf-8",
dataType: 'json',
processData: true,
success: function (response) {
alert('s');
},
error: function (error) {
alert(error.status + ' : ' + error.statusText);
}
})
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public string GetName(string name)
{
return "Result !";
}
}
我的尝试:
上面已经提到了代码。
What I have tried:
The code is already mentioned above.
推荐答案
.support.cors = true ;
.support.cors = true;
.ajax({
url:servicePath,
crossDomain: true ,
类型:' POST',
async: false ,
cache: false ,
data:' {name:xyz}',
contentType: application / json; charset = utf-8,
dataType:' json',
processData: true ,
成功:功能(re赞助){
alert(' s');
},
错误: function (错误){
alert(error.status + ' :' + error.statusText);
}
})
.ajax({ url: servicePath, crossDomain: true, type: 'POST', async: false, cache: false, data: '{"name": "xyz"}', contentType: "application/json; charset=utf-8", dataType: 'json', processData: true, success: function (response) { alert('s'); }, error: function (error) { alert(error.status + ' : ' + error.statusText); } })
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public string GetName(string name)
{
return "Result !";
}
}
我的尝试:
上面已经提到了代码。
What I have tried:
The code is already mentioned above.
这篇关于如何使用参数从jquery ajax调用跨域WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文