最简单的jQuery .ajax无法找到json文件

时间:2013-02-11 19:00:45

标签: jquery json iis

我在IIS目录中托管了以下jQuery:

$(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 个答案:

答案 0 :(得分:6)

由于这是IIS,因此您可能会从缺少的MIME映射中获得404错误的子类型,即404.3错误。 JSON文件未注册为IIS默认提供的内容。进入INETMGR并将扩展名添加到MIME地图中。

(网站 - MIME类型 - 添加 - json:application / json)

enter image description here

答案 1 :(得分:0)

还要记住,如果你的文件有扩展名" .json" ,那么它必须包含正确的json对象,否则你的请求将失败......