通过ajax调用webservice方法

我有WCF服务方法:

[WebInvoke(Method = "POST", UriTemplate = "validateLogin", ResponseFormat = WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Bare)] [OperationContract] bool validateLogin(Login objLogin); 

我通过我的phonegap代码ajax调用此方法:

 var parameters = { "EmailID": EmailID, "Password": Password }; $.ajax({ url: "http://localhost:95/MobileEcomm/Service1.svc/validateLogin", data: JSON.stringify(parameters), contentType: "text/xml;charset=utf-8", dataType: "json", headers: { SOAPAction: '' }, type: 'POST', processdata: false, cache: false, success: function (Data) { alert("asdsad"); }, error: function (response) { var value = JSON.stringify(response); alert("Error in Saving.Please try later."+value); } }); 

但服务方法没有被调用。

在网络选项卡上它给我错误:

在此处输入图像描述

在控制台上:

在此处输入图像描述

EDIT1:

当我将contenttyp更改为:appplication / json; charset = utf-8

在此处输入图像描述

http://api.jquery.com/jquery.ajax/

doc中的crossDomain点

检查一下,你发送跨域ajax。 默认情况下不允许这样做。

很可能是因为您传递的参数和返回类型的WCF服务。 该方法应该返回Object而不是bool 。 .Net框架会自动将返回的对象转换为JSON字符串。

服务方面:

  [WebInvoke(Method = "POST", UriTemplate = "validateLogin", ResponseFormat = WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Bare)] [OperationContract] Object validateLogin(String Email, String Password) { //Do your stuff return bool value } 

AJAX电话:

 $.ajax({ url: "http://localhost:95/MobileEcomm/Service1.svc/validateLogin", data: function ( { return JSON.stringify({ Email: "abc@xyz.com", Password: "XXXXXXXXXX" }); }, contentType: "text/xml;charset=utf-8", dataType: "json", headers: { SOAPAction: '' }, type: 'POST', processdata: false, cache: false, success: function (Data) { alert("asdsad"); }, error: function (response) { var value = JSON.stringify(response); alert("Error in Saving.Please try later."+value); } });