$.getJSON 解析器错误试图调用 API [英] $.getJSON parsererror trying to call API

查看:38
本文介绍了$.getJSON 解析器错误试图调用 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use the Clipped API (http://clipped.me/api.html) that returns JSON but am running into some trouble. I'm using getJSON, and in Chrome's JS console I get these error messages:

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

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

请求失败:解析器错误,错误:jQuery19108596111265942454_1364420105378未被调用

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 做一些事情,所以我真的不知道在这里做什么.我试过谷歌搜索,但找不到任何东西.我实际发送的数据被我添加 callback=? 时出现的这个 jQuery 通知截断了?

This is my first time trying to make something with an API or JSON at all, so I'm really not sure what to do here. I've tried Googling around but can't find anything. The data that I'm actually sending is getting cut off by this jQuery notice that appears when I add callback=?

推荐答案

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

Your parameter will not simply "guess" what the [URL] param is. Try this:

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 标头.因此,您有两个选择:

Even this fails, however, as your API endpoint does not seem to understand/support JSONP and does not provide a Access-Control-Allow-Origin header. You therefore have two choices:

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

这篇关于$.getJSON 解析器错误试图调用 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆