简体   繁体   English

无法在jquery中加载json数据

[英]Can't load json data in jquery

I am trying to get json data from twitter . 我想从twitter获取json数据。 http://api.twitter.com/1/statuses/public_timeline.json this is my code http://api.twitter.com/1/statuses/public_timeline.json这是我的代码

<html>
<head>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
    <script>
        $(document).ready(function(){

            $("button").click(function(){
                $.getJSON("http://api.twitter.com/1/statuses/public_timeline.json",function(result){
                    $.each(result, function(i, field){
                        $("div").append(field + " ");
                    });
                });
            });
        });
    </script>
</head>
<body>

<button>Get JSON data</button>
<div></div>

</body>
</html> 

it is from http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_ajax_getjson but it is not working ! 它来自http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_ajax_getjson,但它不起作用!

You are running in the same origin policy in JavaScript. 您在JavaScript中使用相同的源策略 This basically says, that you can't access resources from different domains (in your case the other domain would be twitter.com). 这基本上说,你不能从不同的域访问资源(在你的情况下,其他域将是twitter.com)。

One solution is to use JSONP. 一种解决方案是使用JSONP。 The twitter API supports this . twitter API支持这一点 You can run a JSONP-request with jQuery in your case like this: 您可以在这种情况下使用jQuery运行JSONP请求,如下所示:

$.ajax({
  url: "http://api.twitter.com/1/statuses/public_timeline.json",
  cache: false,
  dataType: 'jsonp',
  success: function( result ){
    $.each(result, function(i, field){
                        $("div").append(field + " ");
                    });
  }
});

Besides, w3schools is no reliable source for information. 此外,w3schools不是可靠的信息来源。 Better use something like the Mozilla Developer Network . 更好地使用像Mozilla Developer Network这样的东西。

Check out the json document you are trying to load. 查看您要加载的json文档。 It returns the following error when I try to access it: 我尝试访问它时返回以下错误:

{"errors":[{"message":"Sorry, that page does not exist","code":34}]}

I would try replacing the url with a working api call to twitter before you worry about whether the code snippet is working. 在你担心代码片段是否正常工作之前,我会尝试用一个有效的api调用来替换url。 :) :)

Your trying to get a document from a different domain and the requested URL is blocking your request due to Cross-Origin Resource Sharing. 您尝试从其他域获取文档并且请求的URL因Cross-Origin资源共享而阻止您的请求。 You can read up on that here: http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing 你可以在这里阅读: http//en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing

You can use JSONP with jQuery which should allow you to complete the request but that page you are requesting gets a 404 error so there's probably not much you can do with the result anyway. 您可以将JSONP与jQuery一起使用,这应该允许您完成请求,但您请求的页面会出现404错误,因此您可能无法对结果做多少。

You can Access json data from other domain using $.ajax() jquery method .See the code example below 您可以使用$ .ajax()jquery方法从其他域访问json数据。请参阅下面的代码示例

   $.ajax({

   url : " // twitter api url",
   dataType : "JSON",
   cache : false,
   success : function(success)
    {
     $.each(success, function(index,value) {$("div").append(value + ""); })
    }

   error : function() { //Statements for handling ajax errors }

   });

The url you are using in that ajax request is returning a json array which contains an error 您在该ajax请求中使用的url正在返回包含错误的json数组

{"errors": [{"message": "The Twitter REST API v1 is no longer active. Please       
migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.", "code": 68}]}

You need to migrate to API v1.1 before process the request , Replace the twitter api url with the new json url which is provided in API v1.1 您需要在处理请求之前迁移到API v1.1,将twitter api url替换为API v1.1中提供的新json url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM