简体   繁体   中英

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({
   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. In this case, the returned JSON has a semi-colon at the end of the body which is not valid 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' )

You can tell jQuery to parse it as text (using dataType: 'text' ) and then convert it to JSON manually using JSON.parse .

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. You will get parse error, if your json response is not valid and you are using dataType: 'json' . you can change it 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

1.json file is incorrect. 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.

You are receiving the data in error because you are expecting a JSON response where as the actual response is not a valid JSON .

It has semicolon at the end, it makes a invalid JSON .

Try dataType as text . Here you go with the example 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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