如何在我的phonegap项目中访问远程服务器json / html数据? [英] how can i access remote server json/html data in my phonegap project?

查看:111
本文介绍了如何在我的phonegap项目中访问远程服务器json / html数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的手机差距项目,我需要从远程服务器访问json / html数据。我尝试使用Xmlhttprequest,但它不工作。如果有人帮助我将是有帮助的。提前。

In my phone gap project I need to access json / html data from a remote server .I tried with Xmlhttprequest,but it is not working.It will be helpful if somebody helped me.Thanks in advance.

注意:我正在使用php

Note: I am using phone gap with php

推荐答案

用于连接REST风格的API(使用jquery):

Here is some code that I use for making connections to a REST style API (uses jquery):

var api = {
    connection: {
        baseUrl: 'your_domain.tld',
        apiUrl: '/some_path/'
    },
    initialize: function() {
        api.setupAjaxDefaults();
    },
    setupAjaxDefaults: function() {
        var headers = {
            'Accept': "application/json; encoding='utf-8'",
            'Content-Type': "application/json; encoding='utf-8'"
        };
        $.ajaxSetup({
            headers: headers,
            dataType: 'json',
            crossDomain: true
        });
    },
    testCall: function(data) {
        api.ajaxGet( someMethod, data, aSuccessCallback, anErrorCallback );
    },
    ajaxGet: function(methodName, data, successCallback, errorCallback) {
        $.ajax({
            url: api.connection.baseUrl + api.connection.apiUrl + methodName,
            data: data,
            cache: false,
            type: 'GET',
            success: function(result, status, xhr) {
                if ($.isFunction(successCallback)) {
                    successCallback(result);
                }
            },
            error: function() {
                if ($.isFunction(errorCallback)) {
                    errorCallback();
                }
            }

        });
    },
    ajaxGetCached: function(methodName, data, successCallback, errorCallback) {
        $.ajax({
            url: api.connection.baseUrl + api.connection.apiUrl + methodName,
            data: data,
            type: 'GET',
            success: function(result, status, xhr) {
                if ($.isFunction(successCallback)) {
                    successCallback(result);
                }
            },
            error: function() {
                if ($.isFunction(errorCallback)) {
                    errorCallback();
                }
            }

        });
    }
};

要使用这个,你首先调用 api.initialize code>

To use this, You first call api.initialize();

然后拨打电话(上面的testCall示例):

Then to make a call (example of testCall in place above):

api.testCall(someApiMethod, { data: someData, moreData: evenMore });

这篇关于如何在我的phonegap项目中访问远程服务器json / html数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆