我如何在JQuery中处理JSON数据,这是以“text / html”的标题类型返回的? [英] How can I process JSON data in JQuery, that's being returned with a header type of "text/html"?

查看:339
本文介绍了我如何在JQuery中处理JSON数据,这是以“text / html”的标题类型返回的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用jQuery, getJSON 方法从公共API获取JSON数据。我的 getJSON 请求的回调执行时,我总是收到以下错误。


警告:资源解释为脚本,但使用MIME类型text / html传输

错误:未捕获的SyntaxError:意外的令牌:

我检查了响应的标题,果然,它被设置为 text \\ \\html 。由于这是一个我无法控制的公共API,我怎样才能轻松地请求和解析这个JSON数据?



作为参考,下面的链接是指向我试图获取JSON数据。



JSON请求( http://bitcoincharts.com/t/weighted_prices.json

解决方案

如果您不能控制mime类型,使用 jQuery.ajax 而不是 jQuery.getJSON 。然后在成功回调中,您可以执行如下操作:

  jQuery.ajax({
dataType:text ,//你可能需要这个
success:function(data,textStatus,jqXHR){
var jsonData = JSON.parse(data);
....
}
});

您可以尝试的另一件事是:

<$ $ {$ b $ dataType:json,
success:function(data,textStatus,jqXHR){
// data here will be一个JavaScript对象
....
}
});

尽管我不确定如果mime类型不匹配,jQuery是否会投诉。这值得一试。

编辑:在另一张笔记上,您如何能够从您的脚本访问该数据?即使它是一个公共API,它在另一台服务器上,因此会违反同源策略。当我执行以下操作时:

  jQuery.getJSON(http://bitcoincharts.com/t/weighted_prices.json,函数(数据){
console.log(data);
});

在我的Chrome控制台中,我看到以下错误:

  XMLHttpRequest无法加载http://bitcoincharts.com/t/weighted_prices.json?_=1346263039525。 Access-Control-Allow-Origin不允许Origin http://stackoverflow.com。 

您必须查看BitCoin Charts是否支持JSONP备选方案。否则,你唯一的选择是在服务器端设置一些东西来抓取这些数据,然后用JSON以正确的MIME类型返回给你。


I'm trying to use the jquery, getJSON method to get JSON data from a public API. I keep getting the following errors when the callback of my getJSON request executes.

Warning: Resource interpreted as Script but transferred with MIME type text/html

Error: Uncaught SyntaxError: Unexpected token :

I've inspected the header of the response and, sure enough, it is set to text\html. Since this is a public API that I can't control, how can I easily request and parse this JSON data?

For reference, the link below is the link to the JSON data that I am trying to acquire.

JSON Request (http://bitcoincharts.com/t/weighted_prices.json)

解决方案

If you can't control the mime type, use jQuery.ajax instead of jQuery.getJSON. Then in the success callback you can do something like this:

jQuery.ajax({
    dataType: "text", //you may need this.
    success: function(data, textStatus, jqXHR) {
        var jsonData = JSON.parse(data);
        ....
    }
});

Another thing you could try is:

jQuery.ajax({
    dataType: "json",
    success: function(data, textStatus, jqXHR) {
        //data here will be a JavaScript object
        ....
    }
});

Although I'm not sure if jQuery will complain if the mime type doesn't match. It's worth a shot though.

EDIT: On another note, how are you able to access that data from your script? Even though it's a public API, it's on another server and so that will violate the same-origin policy. When I do the following:

jQuery.getJSON("http://bitcoincharts.com/t/weighted_prices.json", function(data) {
    console.log(data);
});

From my Chrome console, I see the following error:

XMLHttpRequest cannot load http://bitcoincharts.com/t/weighted_prices.json?_=1346263039525. Origin http://stackoverflow.com is not allowed by Access-Control-Allow-Origin.

You have to see if BitCoin Charts supports a JSONP alternative. Otherwise, your only option is to set up something on the server-side that grabs this data for you and returns it back to you in JSON with the correct mime-type.

这篇关于我如何在JQuery中处理JSON数据,这是以“text / html”的标题类型返回的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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