2013-02-11 27 views
1

我有以下的jQuery在IIS目录托管:最简单的jQuery阿贾克斯无法找到JSON文件

$(function() { 
    $.ajax({ 
     "url": "sample.json", 
     "type": "get", 
     "dataType": "json", 
     "success": function(data) { 
      console.log(data); 
     }, 
     "error": function(jqXHR, status, error) { 
      console.log("status:",status, "error", error); 
     } 
    }) ; 
}); 

当我浏览html文件,我得到这个在JS控制台:

GET http://localhost/Ajax/sample.json 404 (Not Found) jquery-1.9.0.min.js:3 
status: error error Not Found app.js:18 

它在目录中的权利。当我使用/sample.json时,得到:

GET http://localhost/sample.json 404 (Not Found) jquery-1.9.0.min.js:3 
status: error error Not Found 

如何获取它以查找本地文件并执行ajax调用?

+2

你能直接使用浏览器在地址栏中输入URL来访问文件吗?如果是,您使用哪个网址? – Alexander 2013-02-11 19:02:15

+0

是的,但它无法处理由于扩展配置,您请求的页面无法投放。如果页面是脚本,请添加处理程序。如果文件应该被下载,添加一个MIME映射。 http://localhost/Ajax/sample.json – 2013-02-11 19:16:33

+0

然后,现在你知道该怎么做 – Alexander 2013-02-11 19:21:00

回答

6

由于这是IIS,您可能会从缺失的MIME映射中获得404错误的子类型404.3错误。 JSON文件没有被注册为默认情况下IIS会提供的东西。进入INETMGR并将扩展添加到您的MIME地图。

(网站 - MIME类型 - 添加 - JSON:应用/ JSON)

enter image description here

+0

是的,就是这样!谢谢!标出了答案 – 2013-02-11 19:21:29

0

还要记住,如果你的文件已经得到了扩展名 “以.json”,那么它必须包含正确的JSON对象 ,否则你的请求将失败...