Cordova Phonegap - 简单 Jquery 调用

Cordova Phonegap - Simple Jquery Call

我无法进行 wcf 调用。

我正在使用:

有人帮帮我吗?

接口定义

[ServiceContract]
public interface IService
{
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        string GetAddNumbers(int a, int b);
}

服务,采用两个参数和returns一个值

的简单方法
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service : IService
{
    public string GetAddNumbers(int a, int b)
    {
        return (a + b).ToString();
    }
}

服务网络配置

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="EndpBehavior">
          <enableWebScript/>
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="Service">
        <endpoint address="" binding="webHttpBinding" contract="IService" behaviorConfiguration="EndpBehavior" bindingConfiguration="crossdomain"/>
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true">
    </serviceHostingEnvironment>
    <bindings>
      <webHttpBinding>
        <binding name="crossdomain" crossDomainScriptAccessEnabled="true"/>
      </webHttpBinding>
    </bindings>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
        <add name="Access-Control-Max-Age" value="1728000" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

Cordova App - 使用JQuery调用wcf

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link href="Content/jquery.mobile-1.4.5.min.css" rel="stylesheet" />
    <script src="scripts/jquery-2.1.3.min.js"></script>
    <script src="scripts/jquery.mobile-1.4.5.min.js"></script>
    <script src="cordova.js"></script>
    <script>

        //$.support.cors = true;

        document.addEventListener("deviceready", onDeviceReady, false);

        function onDeviceReady() {

            $("button").click(function () {

                var params = { a: $("#first").val(), b: $("#second").val() };

                $.ajax({
                    url: 'http://*******:***/Service.svc/GetAddNumbers',
                    data: params,
                    type: "GET",
                    dataType: "json",
                    success: function (data, status, xr) {
                        $("#results").html(data);
                    },
                    error: function (xr, msg, e) {
                        $("#results").html(msg);
                        alert(msg);
                    }
                });
            });
        }
    </script>
</head>
<body>
    <div>
        <input id="first" type="text" />
        <input id="second" type="text" />
        <button>Call Wcf</button>
        <div id="results"></div>
    </div>
</body>
</html>

跨域代理有 3 个设置:"remote"、"local" 和 "disabled"。

我设置了 "disabled" 并且工作正常。

感谢大家

我在远程计算机上使用 WCF 服务时遇到了同样的问题。我将 cross-domain-proxy 设置从 "local" 更改为 "remote" 和 "disabled",即使我向每个请求添加了 Access-Control-Allow-Origin header,它也不起作用.

我注意到代理端口与 Chrome 地址栏中的端口不同; 一个是4400,另一个是http://localhost:4443/index.html?enableripple=cordova-3.0.0-NexusGalaxy

当我更改代理端口以匹配地址栏中的端口时,调用成功。

如果它们不匹配,错误是:加载资源失败:net::ERR_CONNECTION_REFUSED 文件:xhr_proxy,行:0,列:0