【发布时间】:2020-11-16 15:04:26
【问题描述】:
我可以登录我的网站并通过 POST 从 API 获取令牌。 现在我尝试使用从 API 获取一些数据,但它返回 CORS 错误或 401。我在客户端站点上使用 Vue.js,并使用 ASP.NET Freamwork 4.6 作为 API。 关于 Postman API 的工作。
我的一些 Vue.js 代码:
GetUsers: function(Page = 1){
const URI = "http://localhost:5000/api/user?Page="+Page;
var optionsAxios = {
headers: {
'Authorization': 'Bearer ' + localStorage.getItem("user-token"),
'Content-Type': 'application/x-www-form-urlencoded'
}
}
//var querystring = require('querystring');
axios.get(URI, optionsAxios).then((result) =>{
console.log(result.results)
}).catch(err => {
console.log(err)
})
},
在我添加的 API 中的 Web.config 中
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:8080" />
</customHeaders>
</httpProtocol>
现在它回来了 跨域请求被阻止:同源策略不允许读取远程资源
如果我使用
const URI = "http://localhost:5000/api/user
和获取
var querystring = require('querystring');
axios.get(URI,
querystring.stringify({
page: Page
}),
optionsAxios).then((result) =>{
console.log(result.results)
}).catch(err => {
console.log(err)
})
它会返回 错误:请求失败,状态码为 401
编辑
我尝试添加 Web.config
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:8080" /> <-- This one I had before
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept, Pragma, Cache-Control, Authorization " />
<add name="Access-Control-Allow-Methods" value="*"/>
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
无论采用何种组合,都没有改变。
我尝试的下一个是
var cors = new EnableCorsAttribute("http://localhost:8080", "*", "*");
config.EnableCors(cors);
在 WebApiConfig.cs 中 在这个我什至不能在网站上登录。
谷歌浏览器显示: 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态。
Firefox 显示: 跨源请求被阻止:同源策略不允许读取位于 http://localhost:5000/api/user?Page=5 的远程资源。 (原因:CORS 预检响应未成功)
和
跨域请求被阻止:同源策略不允许读取位于 http://localhost:5000/api/user?Page=5 的远程资源。 (原因:CORS 请求未成功)。
EDIT2
我修复了这个问题,可能不是最好的方法,但它确实有效。我需要将我找到的所有方法合二为一。
在 Web.config
中<httpProtocol>
<customHeaders>
<!--<add name="Access-Control-Allow-Origin" value="http://localhost:8080" />-->
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept, Pragma, Cache-Control, Authorization " />
<add name="Access-Control-Allow-Methods" value="*"/>
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
我评论了允许来源。 在 WebApiConfig.cs
中添加var cors = new EnableCorsAttribute("http://localhost:8080", "*", "*");
config.EnableCors(cors);
在 Global.asax/Global.asax.cs 中
protected void Application_BeginRequest(Object sender, EventArgs e)
{
// Preflight request comes with HttpMethod OPTIONS
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
// The following line solves the error message
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
// If any http headers are shown in preflight error in browser console add them below
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Pragma, Cache-Control, Authorization ");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
然后一切都修复了。 肯定是代码太多了。
【问题讨论】:
-
应用程序是从哪个 URL 下载的?
-
没有下载
-
这个错误可能是因为你的令牌@DonQnei
-
我在 Postman 中尝试过这个令牌,它奏效了。
-
未下载 - 它是如何进入浏览器的?即使浏览器从 localhost 上运行的 Web 服务器下载应用程序,它仍在下载该应用程序..
标签: vue.js asp.net-web-api axios cors