科尔多瓦的PhoneGap - 简单的jQuery呼叫 [英] Cordova Phonegap - Simple Jquery Call
本文介绍了科尔多瓦的PhoneGap - 简单的jQuery呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不能让WCF电话。
我使用的是:
- WCF
- Visual Studio的科尔多瓦
有人帮帮我吗?
接口定义
[的ServiceContract]
公共接口IService
{
[OperationContract的]
[WebInvoke(方法=GET,ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json)
字符串GetAddNumbers(INT A,INT B);
}
服务,简单的方法,它有两个参数,并返回一个值
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)
公共类服务:IService
{
公共字符串GetAddNumbers(INT A,INT B)
{
返回(A + B)的ToString();
}
}
服务Web配置
<?XML版本=1.0&GT?;
<结构>
<&的System.Web GT;
<编译targetFramework =4.5/>
<的httpRuntime targetFramework =4.5/>
< /system.web>
< system.serviceModel>
<&行为GT;
< serviceBehaviors>
<行为NAME =ServiceBehavior>
< serviceMetadata httpGetEnabled =真/>
< serviceDebug includeExceptionDetailInFaults =FALSE/>
< /行为>
< / serviceBehaviors>
< endpointBehaviors>
<行为NAME =EndpBehavior>
< enableWebScript />
< webHttp />
< /行为>
< / endpointBehaviors>
< /行为>
<服务和GT;
<服务behaviorConfiguration =ServiceBehaviorNAME =服务>
<端点地址=绑定=的WebHttpBinding合同=IServicebehaviorConfiguration =EndpBehaviorbindingConfiguration =跨域/>
< /服务>
< /服务>
< serviceHostingEnvironment aspNetCompatibilityEnabled =假multipleSiteBindingsEnabled =真>
< / serviceHostingEnvironment>
<&绑定GT;
<&的WebHttpBinding GT;
<绑定名称=跨域crossDomainScriptAccessEnabled =真/>
< /&的WebHttpBinding GT;
< /绑定>
< /system.serviceModel>
< system.webServer>
<模块runAllManagedModulesForAllRequests =真/>
< httpProtocol>
< customHeaders>
<添加名称=访问控制允许来源VALUE =*/>
<添加名称=访问控制 - 允许 - 头VALUE =Content-Type的,接受/>
<添加名称=访问控制允许的方法VALUE =POST,GET,OPTIONS/>
<添加名称=访问控制-max-age的值=1728000/>
< / customHeaders>
< / httpProtocol>
< /system.webServer>
< /结构>
科尔多瓦应用 - 使用jQuery WCF
打电话 <!DOCTYPE HTML>
< HTML和GT;
< HEAD>
<标题>< /标题>
<链接HREF =内容/ jquery.mobile-1.4.5.min.css的rel =stylesheet属性/>
<脚本SRC =脚本/ jQuery的-2.1.3.min.js>< / SCRIPT>
<脚本SRC =脚本/ jquery.mobile-1.4.5.min.js>< / SCRIPT>
&所述; SCRIPT SRC =cordova.js>&下; /脚本>
<脚本> //$.support.cors = TRUE; document.addEventListener(deviceready,onDeviceReady,FALSE); 功能onDeviceReady(){ $(按钮)。点击(函数(){ VAR PARAMS = {A:$(#第一)VAL(),B:$(#秒)VAL()}; $阿贾克斯({
网址:HTTP:// *******:*** / Service.svc / GetAddNumbers',
数据:参数,可以
键入:GET,
数据类型:JSON
成功:功能(数据,状态,XR){
$(#结果)HTML(数据);
},
错误:功能(XR,味精,E){
$(#结果)HTML(MSG);
警报(MSG);
}
});
});
}
< / SCRIPT>
< /头>
<身体GT;
< DIV>
<输入ID =第一类型=文本/>
<输入ID =第二类型=文本/>
<按钮和GT;调用WCF< /按钮>
< DIV ID =结果>< / DIV>
< / DIV>
< /身体GT;
< / HTML>
解决方案
跨域代理有3种设置:远程,本地和禁用
我设置禁用和工作的罚款。
感谢所有
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>
解决方案
The cross-domain-proxy has 3 settings: "remote", "local" and "disabled".
I set "disabled" and work's fine.
thanks to all
这篇关于科尔多瓦的PhoneGap - 简单的jQuery呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文