getJSON不会在firefox扩展中返回任何内容

时间:2011-10-29 17:33:36

标签: jquery firefox firefox-addon getjson wikipedia-api

我正在尝试使用jquery编写一个firefox扩展来从wikipedia API获取数据。我这样称呼getJSON:

jqxhr = $.getJSON("http://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?", {page:wikipediaPage, prop:"text|images", uselang:"en"}, function() {dump("success?\n");} ).error(function() { dump("error?\n"); } );

此代码正在执行,因为脚本没有停止运行,我没有看到异常,但是成功或错误功能都没有响应。当我将它放在我的计算机上的HTML文档中并从浏览器运行时,代码就可以工作,但是在扩展程序中它失败了。

我对javascript和jquery都很陌生,所以任何帮助都会非常感激!

以下是作为网页加载时有效的代码:

<html>
<head>
    <style>
        a.test {font-weight : bold; }
    </style>
</head>
<body>

    <a href="http://jquery.com/">jQuery</a>
    <script src="jquery.js"></script>
    <div id="insertTest"></div>

    <script>
        var wikipediaHTMLResult = function(data) {
            document.write("<p>Inside callback</p>");
            document.write("Text data: " + data);
            var readData;
            for(x in data){
                for(y in data[x]){
                    for(z in data[x][y]){
                        readData += data[x][y][z];
                    }
                }
            }
            var arrayData = jQuery.makeArray(readData);
            var newData = new Array();
            var ignore = false;
            //document.write("<p>" + readData + "</p>");
            var i = 0;
            //document.write(arrayData);
            for(x in readData){
                if(i > 100000){
                    break;
                }
                i++;
                if(readData[x]=== '<'){
                    ignore = true;
                }
                else if(readData[x] === '>'){
                    ignore = false;
                }
                else if(readData[x]==='"'){
                    if(ignore){
                        newData[x] = "";
                    }
                    else{
                        newData[x] = "&quot;";
                    }
                }
                else{
                    if(ignore){
                        newData[x] = "";
                    }
                    else{
                        newData[x] = readData[x];
                    }
                }
            }
            i = 0;
            for(x in newData){
                document.write(newData[x]);
                i++;
            }
            document.write("<p>"+i+"</p>");
        };
        function callWikipediaAPI(wikipediaPage) {
            $.getJSON('http://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?', {page:wikipediaPage, prop:'text|images', uselang:'en'}, wikipediaHTMLResult);
        }
        callWikipediaAPI('Gregg Hartsuff');


        $("a").addClass("test");
        $(document).ready(function(){
            $("a").click(function(event){
                event.preventDefault();
                $(this).hide("slow");
            });
        });
        document.write("<p>Script is running</p>");
    </script>

</body>

2 个答案:

答案 0 :(得分:0)

这可能有所帮助,我有兴趣看看API会返回什么,所以我掀起了一个简单的例子。你可以在这里看到小提琴:http://jsfiddle.net/neilheinrich/ZPNxv/

此外,您一定要查看Firebug因为它可以帮助您更轻松地调试它。您可以直接从firebug运行请求(并查看它返回的内容)。

var url = "http://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?";
var page = "baseball"

$.getJSON(url, { 
  page: page, 
  prop:"text|images", 
  uselang:"en"
}, function(data) {
  var $container = $("body")

  // append title
  $container.append(data['parse']['title']);

  // append page text
  $container.append(data['parse']['text']['*']);    

  // append images
  var images = data['parse']['images'];
  $.each(images, function(i, src){
    // note: the path of these images is somewhat obscured
    // filenames are all that is supplied (no path)
    $container.append(src)
  })
})

答案 1 :(得分:0)

YES! 我也看过这个,并且在网上也没有发现它。 我使用php返回JSOn结果,发现使用header()更改'Content-Type'有帮助,但奇怪的是,我不是每次都会说95%的时间。

另外请确保您的JSON有效,如果有疑问,请将响应复制并粘贴到此处,因为jQuery Firebug根本不执行任何操作: http://jsonformatter.curiousconcept.com/

希望有所帮助