使用Wikipedia API从回调中获取未被捕获的引用错误?

时间:2014-06-15 04:46:49

标签: javascript jquery ajax google-chrome jsonp

因此,当我运行我的一组代码独立于Chrome扩展框架工作,也就是在一个虚拟的html文件中,它工作正常,但是当我尝试用chrome扩展时,维基百科给了我这个奇怪的回调函数添加到我的API网址。

http://en.wikipedia.org/w/api.php?action=query&titles=Girdling&format=json&prop=extracts&exintro=0&redirects&callback=jQuery1111023737464868463576_1402806465025

我在AJAX通话中实际运行的是什么。

    var wikiUrl = "http://en.wikipedia.org/w/api.php?action=query&titles=Girdling&format=json&prop=extracts&exintro=0&redirects&callback=?"
    $.ajax(wikiUrl,{  
        dataType: 'jsonp',
        cache: true,
        success: function(data){
            //do things with the response data
        }

我在控制台中收到此错误:

Uncaught ReferenceError: jQuery1111023737464868463576_1402806465025 is not defined

您可能怀疑JSON数据包含在此函数中:

jQuery1111023737464868463576_1402806465025({"query":{"pages":{"1822951":{"pageid":1822951,"ns":0,"title":"Girdling","extract":"<p><b>Girdling</b>, also called <b>ring barking</b> or <b>ring-barking</b>, is the complete removal of a strip of bark (consisting of cork cambium, phloem, cambium and sometimes going into the xylem) from around the entire circumference of either a branch or trunk of a woody plant. Girdling results in the death of the entire tree over time. A branch completely girdled will fail and when the main trunk of a tree is girdled, the entire tree will die, if it cannot regrow from above to bridge the wound.</p>\n<p>Among the causes of girdling are human practices, including forestry, horticulture, and vandalism. Foresters use the practice of girdling to thin forests and orchardists use it as a cultural technique to yield larger fruit. Girdling can also be caused by herbivorous mammals feeding on plant bark and by birds and insects, both of which can effectively girdle a tree by boring rows of adjacent holes.</p>\n<p></p>"}}}})

那么我怎么告诉维基百科API我希望它运行成功函数,而不是其他函数?我已经尝试在我的网址中设置callback = success。

1 个答案:

答案 0 :(得分:1)

以某种方式修复了它?我认为大多数现代网站中包含的一些协议,但不在我的dummy.html网站中,是否必须包含它的请求类型? (即使它应默认为GET?)

AJAX:

var wikiUrl = "http://en.wikipedia.org/w/api.php?action=query&titles="+wikiTitle+"&format=json&prop=extracts&exintro=0&redirects"
    $.ajax(wikiUrl,{  
       type:"GET",
       cache: true,
       success: function(data){
          //do stuff

我还从网址末尾删除了callback=?。那可能是这两者的组合为我做的。