来自本地的json文件的ajax请求

时间:2017-02-26 03:09:04

标签: javascript html json ajax ajax-request

我想通过AJAX请求检索我的本地JSON文件数据。当我在Chrome中启动html时,会产生错误:

  

XMLHttpRequest无法加载。交叉源请求仅支持协议方案:http,数据,chrome,chrome-extension,https,chrome-extension-resource。

以下是我的代码:

function getJSON(){
                $.ajax({
                    url: "C:/Users/test/Documents/Demo/KendoTest/testJS/test.json",
                    type: "GET",
                    crossDomain: true,
                    success:function(res){
                      //do something if successful
                    },
                    error:function(e, msg){
                        alert(JSON.stringify(e) + " " + msg);
                    }
                });
            }

如果我添加dataType : 'jsonp',

它返回错误函数。

1 个答案:

答案 0 :(得分:0)

如果您使用的是Google Chrome,请将此扩展程序添加到您的Chrome中。 在添加切换功能后,启用跨源资源共享'

https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

希望它有所帮助...