2013-02-11 24 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對象 ,否則你的請求將失敗...