Ajax没有通过phonegap调用服务方法

时间:2014-11-20 09:30:37

标签: javascript c# jquery .net ajax

我有一个ajax调用:

$.ajax({
        type: "POST",
        url: "http://localhost:95/MobileEcomm/Service1.svc/validateLogin",
        crossDomain: true,
        data:{ 'EmailID':EmailID, 'Password':Password},
        success: function (data) {
        alert(data);
            // do something with server response data
        },
        error: function (err) {
            // handle your error logic here
            alert("Error");
        }
    });

我想调用:

[WebInvoke(
  Method = "POST",
  BodyStyle = WebMessageBodyStyle.Wrapped,
  ResponseFormat = WebMessageFormat.Json,
  RequestFormat = WebMessageFormat.Json,
  UriTemplate = "validateLogin/{Email}/{Password}")]
        [OperationContract]
        string validateLogin(string Email, string Password);

但我总是进入ajax的错误块。

我的ajax电话中有什么错误?

ERROR1:

enter image description here

错误2:

enter image description here

2 个答案:

答案 0 :(得分:1)

1)当应用程序在file://协议上运行部分或全部文件时,存在许多难以捕获的错误。建议您完全在http://上调试您的应用。如果具有file://协议的所有URI都与Ripple仿真器相关,请参阅下一点。

2)有一个名为Same-origin policy(CORS)的限制。由于您的应用似乎在localhost:58889上运行且被叫服务器位于localhost:95,因此这两个服务器被视为不同的服务器。 Ripple仿真器有一些绕过CORS的代理,试试打开它。我过去也成功地使用了this Chrome extension和Ripple模拟器。

注意:状态:失败类型:待定不属于contentType不匹配,但99%是CORS问题。

答案 1 :(得分:0)

请尝试将contentType添加到您的通话中

$.ajax({
        type: "POST",
        url: "http://localhost:95/MobileEcomm/Service1.svc/validateLogin",
        crossDomain: true,
        contentType: 'application/json',
        data:{ 'EmailID':EmailID, 'Password':Password},
        success: function (data) {
        alert(data);
            // do something with server response data
        },
        error: function (err) {
            // handle your error logic here
            alert("Error");
        }
    });