尝试调用API的$ .getJSON parsererror

时间:2013-03-27 21:41:43

标签: javascript json api getjson

我正在尝试使用返回JSON的Clipped API(http://clipped.me/api.html),但遇到了一些麻烦。我正在使用getJSON,在Chrome的JS控制台中,我收到以下错误消息:

  

资源被解释为脚本但以MIME类型text / html传输:“http://clipped.me/algorithm/clippedapi.php?url=callback=jQuery1910859611126 ... emo-day-2013-still-looking-for-next-airbnb-or-dropbox /& _ = 1364420105379 ”

     

未捕获的SyntaxError:意外的标识符

     

请求失败:parsererror,错误:jQuery19108596111265942454_1364420105378未被调用

这是我的JS:

var clippedAPI = "http://clipped.me/algorithm/clippedapi.php?url=[URL]callback=?";
    $.getJSON(clippedAPI, "http://pandodaily.com/2013/03/26/y-combinator-demo-day-2013-still-looking-for-the-next-airbnb-or-dropbox/" ).done(function(json) {
            console.log("JSON Data: " + json.title );
    }).fail(function(jqxhr, textStatus, error){
            var err = textStatus + ', ' + error;
            console.log("Request Failed: " + err);
    });

这是我第一次尝试用API或JSON制作东西,所以我真的不知道该怎么做。我试过谷歌搜索但找不到任何东西。我实际上发送的数据被我在添加回调时显示的jQuery通知中断了?

1 个答案:

答案 0 :(得分:2)

您的参数不会简单地“猜测”[URL]参数是什么。试试这个:

var clippedAPI = "http://clipped.me/algorithm/clippedapi.php";
$.ajax({
url: clippedAPI,
type: "GET",
dataType: "JSONP",
data: {
url: "http://pandodaily.com/2013/03/26/y-combinator-demo-day-2013-still-looking-for- the-next-airbnb-or-dropbox/"}
}).done(function(json) {
        console.log("JSON Data: " + json.title );
}).fail(function(jqxhr, textStatus, error){
        var err = textStatus + ', ' + error;
        console.log("Request Failed: " + err);
});

然而,即使这样也失败了,因为您的API端点似乎不理解/支持JSONP并且不提供Access-Control-Allow-Origin标头。因此,您有两个选择:

  • 您可以在本地反向代理API以解决跨域问题并通过标准JSON
  • 你可以...嗯...获得更好的API?与开发者订票,以便对其进行分类。
相关问题