【问题标题】:how to use ajax to get a json file in github如何使用ajax在github中获取json文件
【发布时间】:2017-12-25 12:58:15
【问题描述】:

我想用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");
    }
});

但我总是得到“错误”,我检查网络,它获取数据

如何解决这个问题,谢谢!

【问题讨论】:

    标签: javascript jquery json ajax


    【解决方案1】:

    由于您在请求中包含了dataType:'json',,jQuery 将验证返回的 JSON。在这种情况下,返回的 JSON 在正文末尾有一个分号,这不是有效的 JSON。

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

    删除分号以防止错误处理程序被调用。

    【讨论】:

      【解决方案2】:

      我认为您遇到了导致问题的解析错误,您可以修复 json 响应,也可以从您的请求中删除数据类型 json。 如果您的 json 响应无效并且您使用的是 dataType: 'json',您将收到解析错误。你可以改变它 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);
              }
          });
      

      参考:jQuery returning "parsererror" for ajax request

      【讨论】:

        【解决方案3】:

        1.json 文件不正确。最后有一个分号。去掉那个分号就可以正常工作了。

        如果您无权访问该文件,则可以使用以下代码。

        $.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");
            }
        });
        

        在这里,我基本上获取字符串并修剪其最后一个字符,然后将字符串解析回 JSON 对象。

        【讨论】:

          【解决方案4】:

          您收到的数据有误,因为您期望得到 JSON 响应,而实际响应不是有效的 JSON

          最后有分号,它会产生一个无效的JSON

          试试 dataType as text。这里以 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>

          【讨论】:

            【解决方案5】:

            基于错误 parsererror,url 似乎没有返回有效的 JSON,而调用期望它是 JSON (dataType: 'json')

            您可以告诉 jQuery 将其解析为文本(使用 dataType: 'text'),然后使用 JSON.parse 手动将其转换为 JSON。

            您必须在解析之前删除最后一个 ;

            附带说明,您可以使用传递给错误回调的参数来打印错误。

            固定代码:

            $.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>

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-08-13
              • 1970-01-01
              • 2020-09-07
              • 2017-09-08
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多