简体   繁体   English

JSONP适配器Phonegap项目不起作用

[英]JSONP adapter Phonegap project not working

I am using the sample code (slightly modified) to implement a JSONP adapter found here: http://coenraets.org/blog/2013/04/building-pluggable-and-mock-data-adapters-for-web-and-phonegap-applications/ 我正在使用示例代码(略作修改)来实现在此处找到的JSONP适配器: http : //coenraets.org/blog/2013/04/building-pluggable-and-mock-data-adapters-for-web-and- PhoneGap的应用/

My modified in-memory adapter works, but when I try to change from using the mock data, to a JSONP data object from a remote server, it doesn't work. 我修改后的内存适配器可以工作,但是当我尝试从使用模拟数据更改为从远程服务器转换为JSONP数据对象时,它不起作用。 Below is my memory adapter: 以下是我的内存适配器:

var JSONPAdapter = function() {

this.initialize = function(data) {
    var deferred = $.Deferred();
    url = data;
    deferred.resolve();
    return deferred.promise();
}

this.findById = function(id) {
    return $.ajax({url: url + "/" + id, dataType: "jsonp"});
}

this.findByName = function(searchKey) {
    return $.ajax(
        {
            url: url + "?Name=" + searchKey,
            dataType: "jsonp",
            success: function (data) { },
            error: function (XHR, textStatus, errorThrown) { alert("error: " + errorThrown + '\nurl: ' + url + "?Name=" + searchKey);
        }
    });
}

this.getAll = function getAll() {
    return $.ajax({ url: url, dataType: "jsonp" });
}

var url;


}

You don't need the /callback=? 您不需要/ callback =吗? appended to the end of the url. 附加到网址末尾。 This is taken care of automatically because the dataType is set to 'jsonp'. 这是自动处理的,因为dataType设置为'jsonp'。

I suspect this is due to the scope of your JSONData variable. 我怀疑这是由于您的JSONData变量的范围。 It is not initialised correctly if there is an error within the getJSONData() call. 如果getJSONData()调用中存在错误,则无法正确初始化。 Try declaring the JSONData variable before the function is defined. 在定义函数之前,请尝试声明JSONData变量。

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

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