jQuery解析Twitter jSON但不解析我的同一个PHP文档

时间:2012-01-15 10:57:44

标签: php javascript jquery json jquery-mobile

我有一个解析Twitter搜索结果并放入我的HTML文档的函数。代码有效!

这是代码。

<script>
 $(function update_twit(){
$("#notice_div").html('Updating..'); 
$.ajax({
url: "http://search.twitter.com/search.json?q=stackoverflow&rpp=2",

    dataType: 'jsonp',
    success: function(json_results){
        $("#notice_div").html(''); 
        $("#twitList").html('');
        console.log(json_results);
        $('#twitList').append('<ul data-role="listview" data-inset="true" data-theme="c"></ul>');
        listItems = $('#twitList').find('ul');
        $.each(json_results.results, function(key) {
            html = '<p class="ui-li-bside">'+json_results.results[key].text+'</p>';
            html += '<p class="ui-li-aside">Gönderen: <strong>'+json_results.results[key].from_user+'<strong></p>';
            listItems.append('<li><a href="index.html">'+html+'</a></li>');
        });
        // Need to refresh list after AJAX call
        $('#twitList ul').listview();
        window.setTimeout(update_twit, 10000);

    },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        $("#notice_div").html(errorThrown+'Error..'+textStatus);
        window.setTimeout(update_twit, 60000);
    }
});
 })
</script>

问题是我将Twitter网址复制并粘贴到我的浏览器中。得到回应后。我制作了一个类似

的PHP文档(getmyjson.php)
    <?
    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/json');
    $json='{"completed_in":0.021,"max_id":158455057786998785,"max_id_str":"158455057786998785","next_page":"?page=2&max_id=158455057786998785&q=erhan&rpp=2","page":1,"query":"erhan","refresh_url":"?since_id=158455057786998785&q=erhan","results":[{"created_at":"Sun, 15 Jan 2012 07:46:44 +0000","from_user":"_VCG_","from_user_id":169112180,"from_user_id_str":"169112180","from_user_name":"vedat can g\u00FCm\u00FC\u015F","geo":null,"id":158455057786998785,"id_str":"158455057786998785","iso_language_code":"tr","metadata":{"result_type":"recent"},"profile_image_url":"http://a3.twimg.com/profile_images/1710193374/bb_normal.png","profile_image_url_https":"https://si0.twimg.com/profile_images/1710193374/bb_normal.png","source":"&lt;a href=&quot;http://twitter.com/&quot;&gt;web&lt;/a&gt;","text":"@erhan_ordu ben gidemiyom ya ama ke\u015Fke bi ihtimal olsa da gitsem :D bi de VGC DE\u011E\u0130L VCG VCG :d","to_user":"erhan_ordu","to_user_id":458677081,"to_user_id_str":"458677081","to_user_name":"ERHAN ORDU"},{"created_at":"Sun, 15 Jan 2012 07:39:34 +0000","from_user":"hannyfaarah","from_user_id":199700684,"from_user_id_str":"199700684","from_user_name":"H\u2639","geo":null,"id":158453256241168384,"id_str":"158453256241168384","iso_language_code":"in","metadata":{"result_type":"recent"},"profile_image_url":"http://a2.twimg.com/profile_images/1736543252/cats_normal.jpg","profile_image_url_https":"https://si0.twimg.com/profile_images/1736543252/cats_normal.jpg","source":"&lt;a href=&quot;http://twitter.com/&quot;&gt;web&lt;/a&gt;","text":"\"mantan bebep erhan tersayang selalu dihati\" HAHAHAHA emir emir ngakak weeey","to_user":null,"to_user_id":null,"to_user_id_str":null,"to_user_name":null}],"results_per_page":2,"since_id":0,"since_id_str":"0"}';
    echo $json;
    ?>

现在我正在尝试将第一个代码中的Twitter URL更改为www.mywebiste.com/getmyjson.php

但它不起作用!

错误信息是:jQuery164018531796569004655_1326623562322未被调用Error.parsererror

1 个答案:

答案 0 :(得分:0)

为您本地服务器测试切换到通常的json而不是jsonp。所以它变成了:

$.ajax({
    //url: "http://search.twitter.com/search.json?q=stackoverflow&rpp=2",
    //dataType: 'jsonp',
    url: 'getmyjson.php',
    dataType: 'json',
    ...
});

或者如果您仍然想使用jsonp更改您的PHP脚本,如下所示:

$json = '{...}';
echo $_GET['callback'] . '(' .$json . ')';