JSONP adapter Phonegap project not working

こ雲淡風輕ζ 提交于 2019-12-11 18:52:32

问题


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/

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. 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;


}

回答1:


You don't need the /callback=? appended to the end of the url. This is taken care of automatically because the dataType is set to 'jsonp'.




回答2:


I suspect this is due to the scope of your JSONData variable. It is not initialised correctly if there is an error within the getJSONData() call. Try declaring the JSONData variable before the function is defined.



来源:https://stackoverflow.com/questions/24563256/jsonp-adapter-phonegap-project-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!