$ json文件的http.get总是返回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>
+0
谢谢,另一种选择是去IIS管理器,并配置它知道MIME类型中的JSON。 – Yaniv 2014-11-03 07:57:47
答
放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.
这是因为该文件不存在,请尝试输入该地址通过'http://本地主机/ myProject的/ content.json'直接在浏览器中。 – 2014-11-02 13:30:47
该文件在那里,当我已经改变它为txt像Erez在他的答案中说,它的工作 – Yaniv 2014-11-02 13:39:37