簡體   English   中英

$ http.get的json文件總是返回404

[英]$http.get of json file always returns 404

我想在我的應用程序中添加一個配置JSON文件。
我已將它添加到我的項目中並嘗試使用$ http.get獲取它:

$http.get('http://localhost/myProject/content.json').success(function (data) {
        // Do stuff...
}).error((data, status, headers, config) => {
        // Failure...
});

問題是每次我收到錯誤404。

這是您的Web服務器mime類型配置的問題 - 它可能沒有json。 嘗試將文件擴展名重命名為.txt或.html,它應該可以正常工作。

您還可以將mime類型擴展添加到服務器。 對於IIS express,它是web.config。 例如:

 <staticContent>
    <remove fileExtension=".json" />
    <mimeMap fileExtension=".json" mimeType="application/json" />
  </staticContent>

把content.json文件兄弟放到index.html並使用它:

$http.get('content.json').success(function (data) {
        // Do stuff...
}).error((data, status, headers, config) => {
        // Failure...
});
<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
   **<system.webServer>
    <staticContent>
      <mimeMap fileExtension=".json" mimeType="application/json" />
    </staticContent>
  </system.webServer>**
</configuration>


This works fine.. as what Erez A. Korn has said.

在angular 4存儲.json文件到你的項目的根文件夾。 所有Json文件存儲在同一個文件夾中。

暫無
暫無

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

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