本文介绍了使用$ .getJSON()与jQuery的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行以下命令:

  $ .getJSON(http://services.digg.com/2.0/story.getTopNews?limit=25&callback=?'功能(数据){
    的console.log(数据);
});

但我发现了:

story.getTopNews:-1Resource间preTED为脚本,但与MIME类型application / JSON传输。
story.getTopNews:2Uncaught语法错误:意外的令牌:

我也试过这样的事情:

  VAR URL =htt​​p://services.digg.com/2.0/story.getTopNews?limit=25;$阿贾克斯({
    网址:网址,
    跨域:真实,
    数据类型:JSON
    成功:功能(数据,文字,xhqr){
        的console.log(数据);
    }
});

我得到这样的:

XMLHtt prequest无法加载。产地不被访问控制允许来源允许的。
services.digg.com/2.0/story.getTopNews?limit=25GET 未定义(未定义)

任何帮助将大大AP preciated。


解决方案

跨域Ajax请求被的。这意味着你不能在正常的方式做跨域请求,是错误在你的第二个例子的原因。

在第一个例子中,你正在试图解决方法 - JSONP。这个工程通过投入剧本元素插入引用外部脚本的页面。外部脚本必须通过一个函数调用包装中的数据作出反应。如果外部脚本不支持这样做(这似乎Digg的没有)比你不能使用JSONP的解决方法。

由于服务器发送JSON数据(因为它不支持JSONP),浏览器会很困惑,当它试图解析它作为常规的JavaScript。这是你的第一个错误的原因。


看来,Digg的的确实的支持JSONP,但它需要一个额外的参数:

<$p$p><$c$c>$.getJSON('http://services.digg.com/2.0/story.getTopNews?limit=25&type=javascript&callback=?',功能(数据){
    的console.log(数据);
});

这为我工作。

I'm trying to run the following:

$.getJSON('http://services.digg.com/2.0/story.getTopNews?limit=25&amp;callback=?', function(data) {
    console.log(data);
});

But I'm getting:

story.getTopNews:-1Resource interpreted as Script but transferred with MIME type application/json.story.getTopNews:2Uncaught SyntaxError: Unexpected token :

I've also tried something like this:

var url = "http://services.digg.com/2.0/story.getTopNews?limit=25";

$.ajax({
    url: url,
    crossDomain:true,
    dataType: "json",
    success:function(data,text,xhqr) {
        console.log(data);
    }
});

I get this:

XMLHttpRequest cannot load http://services.digg.com/2.0/story.getTopNews?limit=25. Origin http://sumews.com is not allowed by Access-Control-Allow-Origin.services.digg.com/2.0/story.getTopNews?limit=25GET http://services.digg.com/2.0/story.getTopNews?limit=25 undefined (undefined)

Any help would be greatly appreciated.

解决方案

Cross-domain AJAX requests are disallowed by the same-origin policy. This means that you can't do cross-domain requests in the normal way, and is the cause of the errors in your second example.

In the first example, you are trying the workaround -- JSONP. This works by putting a script element into the page that references the external script. The external script must respond by wrapping the data in a function call. If the external script does not support doing this (and it seems digg does not) than you cannot use the JSONP workaround.

Since the server sends JSON data (since it doesn't support JSONP), your browser gets confused when it tries to parse it as regular Javascript. This is the cause of your first errors.


It seems that digg does support JSONP, but it needs an extra argument type=javascript:

$.getJSON('http://services.digg.com/2.0/story.getTopNews?limit=25&type=javascript&callback=?', function(data) {
    console.log(data);
});

This works for me.

这篇关于使用$ .getJSON()与jQuery的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:44