简体   繁体   English

如何使用ajax在github中获取json文件

[英]how to use ajax to get a json file in github

I want to use ajax to get a json file in github, I wrote this code: 我想使用ajax在github中获取一个json文件,我编写了以下代码:

$.ajax({
   url: "https://rawgit.com/nianyuyu/play_javascript/master/1.json",
    type:"get",
    dataType:'json',  
    success: function(data){
      console.log(data);
    },
    error:function() {
      console.log("err");
    }
});

but I always get "err" and I check the network , it gets the data 但是我总是得到“错误”,并且我检查网络,它获取数据 在此处输入图片说明

How can I solve this problem, thank you! 我该如何解决这个问题,谢谢!

Since you have included dataType:'json', in your request, jQuery will validate the returned JSON. 由于您已在请求中包含dataType:'json', ,因此jQuery将验证返回的JSON。 In this case, the returned JSON has a semi-colon at the end of the body which is not valid JSON. 在这种情况下,返回的JSON在主体末尾带有分号,这是无效的JSON。

 {
"name":"Bill Gates",
"street":"Fifth Avenue New York 666",
"age":56,
"phone":"555 1234567"
};

Remove the semi-colon to prevent the error handler from being called. 删除分号以防止错误处理程序被调用。

Based on the error, parsererror , the url doesn't seem to return valid JSON, while the call expects it to be JSON ( dataType: 'json' ) 基于错误parsererror ,URL似乎未返回有效的JSON,而调用则期望它为JSON( dataType: 'json'

You can tell jQuery to parse it as text (using dataType: 'text' ) and then convert it to JSON manually using JSON.parse . 您可以告诉jQuery将其解析为文本(使用dataType: 'text' ),然后使用JSON.parse将其手动转换为JSON。

You'll have to trim out the last ; 您必须修剪掉最后一个; before you parse. 在解析之前。

On a side note, you can use the parameter passed into the error callback to print out the error. 附带说明,您可以使用传递给错误回调的参数来打印错误。

Fixed code: 固定代码:

 $.ajax({ url: "https://rawgit.com/nianyuyu/play_javascript/master/1.json", type: "get", dataType: 'text', success: function(response) { if (!response) return; response = response.slice(0, -1); // trim ";" at the end var data = JSON.parse(response); // convert to object console.log(data); }, error: function(err) { console.log(err); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

I think you are getting a parse error which is causing the issue, you can fix the json response or you can remove data type json from you request. 我认为您遇到了导致问题的解析错误,可以修复json响应,也可以从请求中删除数据类型json。 You will get parse error, if your json response is not valid and you are using dataType: 'json' . 如果您的json响应无效并且您使用的是dataType:'json' ,则会出现解析错误。 you can change it dataType: 'text' 您可以将其更改为dataType:'text'

        $.ajax({
       url: "https://rawgit.com/nianyuyu/play_javascript/master/1.json",
        type:"get",
        dataType: 'text',  
        error: function(data){
        //debugger;
          alert('err');
        },
        success:function(data) {
          alert(data);
        }
    });

Reference: jQuery returning "parsererror" for ajax request 参考: jQuery为ajax请求返回“ parsererror”

1.json file is incorrect. 1.json文件不正确。 At the end there is a semi colon. 末尾有一个半冒号。 Remove that semi colon and it will work fine. 删除该半冒号,它将正常工作。

if you dont have access to that file then you can use the following code. 如果您无权访问该文件,则可以使用以下代码。

$.ajax({
   url: "https://rawgit.com/nianyuyu/play_javascript/master/1.json",
    type:"get",
    dataType:'text',  
    success: function(data){
      console.log(JSON.parse(data.substring(0, data.length - 1)));
    },
    error:function() {
      console.log("err");
    }
});

Here I basically get the string and trim its last character and then parse the string back to JSON object. 在这里,我基本上是获取字符串并修剪其最后一个字符,然后将字符串解析回JSON对象。

You are receiving the data in error because you are expecting a JSON response where as the actual response is not a valid JSON . 您接收到错误的数据,因为您期望的是JSON响应,其中实际响应不是有效的JSON

It has semicolon at the end, it makes a invalid JSON . 它的末尾有分号 ,它使JSON无效

Try dataType as text . 尝试使用dataType作为text Here you go with the example https://jsfiddle.net/sfjxsdsx/1/ 在这里,您将看到示例https://jsfiddle.net/sfjxsdsx/1/

 $.ajax({ url: "https://rawgit.com/nianyuyu/play_javascript/master/1.json", type:"get", dataType:'text', success: function(data){ console.log(data); }, error:function() { console.log("err"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

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

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