【发布时间】:2018-04-05 23:44:57
【问题描述】:
使用 MVC Asp.Net
以下代码适用于实际的用户名和密码。但我不想使用它。 :
WebClient client1 = new WebClient();
client1.Credentials = new NetworkCredential("actualUserName", "actualPassword");
string code1 = client1.DownloadString(@"http://Domainname/GetValue");
尝试了javascript方法:
$.ajax({
type: 'POST',
url: Url,
processData: false,
contentType: 'application/json; charset=utf-8',
dataType: 'jsonp',
crossDomain: true,
jsonpCallback: 'CallBackMethod',
success: function (json) {
console.log(json);
},
error: function (xhr, textStatus, errorThrown) {
console.log(errorThrown);
}
});
但出现以下错误:
Multiple Access-Control-Allow-Origin headers are not allowed for CORS response.
XMLHttpRequest: Network Error 0x80070005, Access is denied.
尝试在 Application_BeginRequest 中的 Global.asax 中添加标头,并在 webconfig 中添加类似条目:
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
以上方法都试过了,在Win7机器上不行。但在Win10机器上工作!!还尝试在服务器中部署并检查,但弹出相同的错误。
【问题讨论】:
标签: javascript asp.net-mvc xmlhttprequest cors networkcredentials