I can not make wcf calls.
I'm using:
- wcf
- visual studio cordova
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>