Cordova Phonegap - 简单的Jquery电话

时间:2015-01-30 11:56:47

标签: android jquery ajax wcf cordova

我无法拨打wcf。

我正在使用:

  • wcf
  • visual studio cordova

有人帮助我吗?

界面定义

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

服务,带有两个参数并返回值的简单方法

[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>

2 个答案:

答案 0 :(得分:2)

跨域代理有3个设置:&#34;远程&#34;,&#34;本地&#34;和&#34;禁用&#34;。

我设置&#34;禁用&#34;工作很好。

感谢所有

答案 1 :(得分:1)

我在远程计算机上遇到了与WCF服务相同的问题。我将跨域代理设置从“本地”更改为“远程”和“禁用”,即使我为每个请求添加了Access-Control-Allow-Origin标头,它也不起作用。

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

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

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