美元.试图调用API时出现错误

$.getJSON parsererror trying to call API

本文关键字:错误 API 调用 美元      更新时间:2023-09-26

我试图使用Clipped API (http://clipped.me/api.html),返回JSON,但我遇到了一些麻烦。我使用getJSON,并在Chrome的JS控制台我得到这些错误信息:

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

Uncaught SyntaxError: Unexpected identifier

请求失败:parsererror, Error: 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通知截断当我添加callback=?

您的参数不会简单地"猜测"[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?向开发人员提交一张票,让他们把它分类。