gpt4 book ai didi

javascript - $.getJSON 解析器错误尝试调用 API

转载 作者:行者123 更新时间:2023-12-02 18:52:36 27 4
gpt4 key购买 nike

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

Resource interpreted as Script but transferred with MIME type 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

Request Failed: parsererror, Error: jQuery19108596111265942454_1364420105378 was not called

这是我的 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 通知 chop 了?

最佳答案

您的参数不会简单地“猜测”[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 header 。因此,您有两个选择:

  • 您可以在本地反向代理 API,以解决跨域问题并通过标准 JSON
  • 您可以...呃...获得更好的 API 吗?向开发人员提交票证以进行排序。

关于javascript - $.getJSON 解析器错误尝试调用 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15670082/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com