Tbseven
Tbseven

Reputation: 120

Cordova Phonegap - Simple Jquery Call

I can not make wcf calls.

I'm using:

Someone help me?

Interface definition

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

Service, simple method that takes two parameters and returns a value

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

Service Web Config

<?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 - Using JQuery to call 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>

Upvotes: 0

Views: 1041

Answers (2)

Bogdan
Bogdan

Reputation: 11

I had the same issue with a WCF service on a remote machine. I changed cross-domain-proxy setting from "local" to "remote" and "disabled" and it didn't work even when I added an Access-Control-Allow-Origin header to every request.

I noticed that the proxy port was different than the port in Chrome address bar; one was 4400, the other one was http://localhost:4443/index.html?enableripple=cordova-3.0.0-NexusGalaxy

When I the changed the proxy port to match the port in the address bar, the call succeeded.

If they don't match the error was: Failed to load resource: net::ERR_CONNECTION_REFUSED File: xhr_proxy, Line: 0, Column: 0

Upvotes: 1

Tbseven
Tbseven

Reputation: 120

The cross-domain-proxy has 3 settings: "remote", "local" and "disabled".

I set "disabled" and work's fine.

thanks to all

Upvotes: 2

Related Questions