且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从AngularJS调用Web API时,以“数据":null,“状态":-1的形式获取响应

更新时间:2023-02-15 07:49:30

您可能需要启用CORS:

You probably need to enable CORS:

在您的WebApiConfig.cs文件中,添加以下内容:

In your WebApiConfig.cs file, add this:

var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);

CORS是浏览器检查的,以在使用脚本进行调用时执行相同的原始策略.使用Fiddler之类的东西或直接在浏览器中访问api时,您将看不到它.

CORS is a check by the browser to enforce same origin policy when the call is made using scripting. You won't see it when using something like Fiddler or when accessing the api directly in the browser.