Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用API-JSON插件从wordpress为phonegap android应用程序正确获取JSON值_Android_Json_Wordpress_Cordova_Jsonp - Fatal编程技术网

如何使用API-JSON插件从wordpress为phonegap android应用程序正确获取JSON值

如何使用API-JSON插件从wordpress为phonegap android应用程序正确获取JSON值,android,json,wordpress,cordova,jsonp,Android,Json,Wordpress,Cordova,Jsonp,你好,我被困在一个android应用程序中,我正试图使用phonegap构建这个应用程序。我尝试构建的应用程序从我在本地安装的wordpress站点检索提要。我已经安装并测试了JSON-API插件,它工作正常 但是当我使用phonegap和eclipse创建android应用程序时,我没有通过javascript加载提要 我已经在projectfolder/assets/www文件夹下创建了一个html文件,其中包含代码 <!DOCTYPE HTML> <html>

你好,我被困在一个android应用程序中,我正试图使用phonegap构建这个应用程序。我尝试构建的应用程序从我在本地安装的wordpress站点检索提要。我已经安装并测试了JSON-API插件,它工作正常

但是当我使用phonegap和eclipse创建android应用程序时,我没有通过javascript加载提要

我已经在projectfolder/assets/www文件夹下创建了一个html文件,其中包含代码

<!DOCTYPE HTML>
<html>
    <head>
        <script src="jquery.js"></script>
        <script src="cordova.js"></script>
        <script>
                jQuery.support.cors = true;
                var target_div = "#contents";
                var nextID = null;
                var prevID = null;
                var api_read_url = "http://127.0.0.1/wordpress_phonegap/?json=get_post&dev=1&id=";
                var init_url = "http://127.0.0.1/wordpress_phonegap/?json=get_post&dev=1&p=1";
                function getID(url) {
                    var str = new String(url);
                    var x = str.split("=");
                    var id = x[1];
                    return id;
                }

            function readSinglePost (url,target_div) {
                var URL = url+"&callback=?";

                console.log(URL);
                jQuery.ajax({
                        url: URL,
                        dataType: "json",
                        jsonp: null,
                        jsonpCallback: null,
                        success: function(data) {
                            alert(data);
                            jQuery(target_div).html("");
                            try{
                                jQuery(target_div).append("<h1>"+data.post.title+"</h1>");
                                jQuery(target_div).append(data.post.content);
                                jQuery(target_div).append("<small>"+data.post.date+"</small>");
                            } catch (e){
                                console.log("error console");
                            } 
                            try {
                                nextID = getID(data.next_url);
                            }
                            catch (e) {
                                ;
                            }
                            try {
                                prevID = getID(data.previous_url);
                            }
                            catch (e) {
                                ; // you should include some of your own error-handling code or
                            }
                } ,
                        error: function(error){
                            console.log("error console");
                        }
            });
            }
            //Renew next and previous buttons
            function getNext(){
                jQuery("#next").click(function(){
                    var id = nextID;
                    var nextURL = api_read_url + id;
                    readSinglePost(nextURL, target_div);
                });
            }
            function getPrevious(){
                jQuery("#previous").click(function(){
                    var id= prevID;
                    var prevURL = api_read_url + id;
                    readSinglePost(prevURL, target_div);
                })
            }

            jQuery(document).ready(function() {
                readSinglePost(init_url, target_div);
                getNext();
                getPrevious();
            });
        </script>
    </head>
    <body>
        <div id="main">
            <button type="button" id="previous">Previous</button>
            <button type="button" id="next">Next</button>
            <div id="title"></div>
            <div id="contents"></div>
        </div>
    </body>
</html>
我知道发生了什么事。由于是交叉浏览器交互,json以jsonp格式发送,因此json值被包装在函数中发送。如何正确获取值


我读过好几篇文章,但不幸的是,我没有找到解决办法。该示例来自WordPress Mobile Applications with PhoneGap一书中的教程。如果服务器以JSONP格式将其发送回,则需要使用JSONP回调来实际使用数据。请阅读这个问题的答案,看看是否有意义:谢谢你的回答。根据您提供的答案并阅读jquery的文档。我发现我可以使用url而不添加+&回调=?;只要我定义dataType=jsonp,因为这个定义会添加回调。这就是我所做的,但我得到了一种新类型的错误[SyntaxError:missing;before语句-status:ok],我不确定发生了什么,但我猜您没有正确地执行JSONP。试着问一个类似的问题:
SyntaxError: invalid label
"status": "ok",