Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从其他站点获取JSON(P)_Javascript_Jquery_Json_Angularjs_Jsonp - Fatal编程技术网

Javascript 从其他站点获取JSON(P)

Javascript 从其他站点获取JSON(P),javascript,jquery,json,angularjs,jsonp,Javascript,Jquery,Json,Angularjs,Jsonp,我正在尝试使用Ajax获取数据。 数据是json。我使用jquery和angular 但结果是未定义或错误 以下是我的jquery代码: $(document).ready(function() { var url = "http://market.dota2.net/history/json/"; $.ajax({ type: 'GET', url: url, async: false,

我正在尝试使用Ajax获取数据。 数据是json。我使用
jquery
angular

但结果是未定义或错误

以下是我的jquery代码:

$(document).ready(function() {
      var url = "http://market.dota2.net/history/json/";

      $.ajax({
           type: 'GET',
            url: url,
            async: false,
            contentType: "application/json",
            dataType: 'jsonp',
            success: function(data) {
              console.log(data);
            }
        });
    });
在Angular中,我使用jsonp方法。怎么了


顺便说一句,在纯Java中,我可以从这个url获取数据…

如果需要json,不要使用
jsonp
,而是
json

$(document).ready(function() {
      var url = "http://market.dota2.net/history/json/";

      $.ajax({
           type: 'GET',
            url: url,
            async: true, /* it fails with false */
            contentType: "application/json",
            dataType: 'json',/* <== here */
            success: function(data) {
              console.log(data);
            }
        });
    });
$(文档).ready(函数(){
变量url=”http://market.dota2.net/history/json/";
$.ajax({
键入:“GET”,
url:url,
async:true,/*它以false失败*/
contentType:“应用程序/json”,

数据类型:'json',/*如果需要json,不要使用
jsonp
而是
json

$(document).ready(function() {
      var url = "http://market.dota2.net/history/json/";

      $.ajax({
           type: 'GET',
            url: url,
            async: true, /* it fails with false */
            contentType: "application/json",
            dataType: 'json',/* <== here */
            success: function(data) {
              console.log(data);
            }
        });
    });
$(文档).ready(函数(){
变量url=”http://market.dota2.net/history/json/";
$.ajax({
键入:“GET”,
url:url,
async:true,/*它以false失败*/
contentType:“应用程序/json”,
数据类型:“json”,/*
怎么了

您试图调用一个提供JSON的端点,就好像它提供了JSONP一样。这是行不通的;它们的格式不同(尽管相关)

例如:

例如:

注意区别:JSONP实际上是一个JavaScript函数调用,围绕JSON进行包装

如果API支持JSONP,则调用支持它的端点

否则,除非提供者支持并与您的源站共享,否则您不能直接查询它,因为这适用于ajax调用

顺便说一下,在纯Java中,我可以从这个url获取数据

因为Java代码不是在SOP控制的上下文中运行的,所以可以从该端点获取数据(作为JSON)并使用它。这也是将URL发布到浏览器地址栏让我们可以看到数据的原因。但是ajax调用受更严格的规则控制

怎么了

您试图调用一个提供JSON的端点,就好像它提供了JSONP一样。这是行不通的;它们的格式不同(尽管相关)

例如:

例如:

注意区别:JSONP实际上是一个JavaScript函数调用,围绕JSON进行包装

如果API支持JSONP,则调用支持它的端点

否则,除非提供者支持并与您的源站共享,否则您不能直接查询它,因为这适用于ajax调用

顺便说一下,在纯Java中,我可以从这个url获取数据


因为Java代码没有在SOP控制的上下文中运行,所以可以从该端点获取数据(作为JSON)这也是将URL发布到浏览器地址栏让我们看到数据的原因。但是ajax调用受更严格的规则约束。

您确实理解JSON和的区别,对吧?示例中的URL不会返回JSONP。
contentType:“application/JSON”,
-您发出GET请求时,没有请求正文来描述的内容类型。您确实了解JSON和的区别,对吗?示例中的URL不返回JSONP。
contentType:“application/JSON”,
-您发出GET请求时,没有请求正文来描述的内容类型。
callback({"foo":"bar"})