简体   繁体   English

带有WCF休息服务的PhoneGap

[英]PhoneGap with WCF Rest Service

So I have started with PhoneGap/Cordova (windows phone) and WCF Rest Services, however, I am having problems on getting the Emulator to interact with the WCF Service. 因此,我已经开始使用PhoneGap / Cordova(Windows Phone)和WCF Rest Services,但是,在使仿真器与WCF服务进行交互时遇到了问题。

At first I was thinking that maybe the Emulator could not connect to the localhost WCF Service, so I published the WCF Service on an external host however the problem still occured... ie still not able to make a call to the WCF Service. 起初,我以为仿真器可能无法连接到本地WCF服务,因此我在外部主机上发布了WCF服务,但是问题仍然存在……即仍然无法调用WCF服务。

The code I have is shown below: 我的代码如下所示:

The javascript file for the PhoneGap application is the following: PhoneGap应用程序的javascript文件如下:

function getAjax() {
var jqxhr = $.ajax({
    url: 'http://link.to.service.com/service1/',
    //headers:
    beforeSend: function (xhr) {
        //xhr.overrideMimeType('text/plain; charset=x-user-defined');
    },
    dataType: 'json'
})
.done(function (data) {
    var element = document.getElementById('ajaxCall');
    element.innerHTML = JSON.stringify(data, null, "\t");
})
 .fail(function (xhr, status, error) {
     showError(error);
 })
 .always(function () { showAlert("complete"); });

} }

Then the WCF Service contains the following method: 然后,WCF服务包含以下方法:

 [WebGet(UriTemplate = "")]
    public List<SampleItem> GetCollection()
    {
        return new List<SampleItem>()
                   {
                       new SampleItem()
                       {
                           Id = 1,
                           StringValue = "Hello" }
                   };
    }

Therefore, when the a call to the javascript method "getAjax" is done, the WCF Service method should be called however it keeps entering the fail function instead showing an error message 'undefined'. 因此,当完成对javascript方法“ getAjax”的调用时,应调用WCF服务方法,但是该方法将继续输入fail函数,而不显示错误消息“ undefined”。

Is there something that I am missing out here? 我这里缺少什么吗?

After further investigation, this has been resolved using the line: 经过进一步调查,已使用以下行解决了此问题:

jQuery.support.cors = true; jQuery.support.cors = true;

Just a small quote from the jQuery library for those having the same issue, this is what the above line does: 对于那些有相同问题的人,只需从jQuery库中引用一小段,这就是上面的代码:

cors is equal to true if a browser can create an XMLHttpRequest object and if that XMLHttpRequest object has a withCredentials property. 如果浏览器可以创建XMLHttpRequest对象,并且该XMLHttpRequest对象具有withCredentials属性,则cors等于true。 To enable cross-domain requests in environments that do not support cors yet but do allow cross-domain XHR requests (windows gadget, etc), set $.support.cors = true;. 要在尚不支持cors但允许跨域XHR请求(Windows小工具等)的环境中启用跨域请求,请设置$ .support.cors = true;。 CORS WD 海盗船

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM