簡體   English   中英

無法從JSON文件獲取數據

[英]Not getting data from JSON file

我正在執行以下代碼:

var element=null;
$.ajax({
        type: 'GET',
        async: false,
        url: "C:\Users\myDir\Desktop\Project\jsonfile.json",
        dataType: 'json',
        success : function(data) {
        element=data;
        }
    });

JSON結構:

{
   "info":[
      {
         "a1": "Ram",
         "b1": "P123"
      },
      {
         "a1": "ROM",
         "b1": "P245"
      }
     ]
}

但是我什么都沒變

使用以下命令檢查ajax中的任何錯誤

var element=null;
$.ajax({
        type: 'GET',
        async: false,
        url: "jsonfile.json",//Edited
        dataType: 'json',
        success : function(data) {
        element=data;
        }
        error: function(jqXHR, textStatus, errorThrown) {
         console.log(textStatus, errorThrown);
        }
    });

可能是文件的權限問題。 將json文件與所有其他代碼文件一起插入,並在url中提供相對路徑

$.ajax({
        type: 'GET',
        async: false,
        url: "jsonfile.json",
        dataType: 'json',
        success : function(data) {
        element=data;
        }
    });

// json文件的位置與進行ajax調用的文件相同

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM