【发布时间】:2019-04-13 17:14:28
【问题描述】:
我有一个托管在 IIS 10.0 上的 ASP .NET MVC 4 网站。控制器中的操作方法验证输入,如果输入不正确,操作方法将返回错误请求状态代码 (400) 以及错误消息。代码如下
public ActionResult SaveCustomer(Customer customer){
var message = string.Empty
var isValid = ValidateCustomer(customer, ref message);
if (!isValid)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest,message);
}
//Remaining code to save customer
}
客户端代码使用 JQuery AJAX 请求,如下所示
var customer = {};
// Code to fill the customer object
$.post('/customer/savecustomer', customer, function (data) {
alert('Customer saved');
}).error(function (response) {
alert(response.statusText);
});
只要站点使用http,response.statusText就会显示action方法中设置的错误信息。但是,一旦站点配置了 https,response.statusText 仅返回“错误”。 我尝试使用此处提到的解决方案,但它不起作用。 ASP.NET MVC 5 ajax error statusText is always "error"
当我在 Chrome 开发者工具中打开网络选项卡时,我发现如下:
启用 HTTP
-
标题标签
请求网址:http://example.com/Customer/SaveCustomer
请求方法:POST
状态码:400 从服务器发送的错误消息
远程地址:xxxx.xxxx.xxxx.xxxx:80
推荐人政策:no-referrer-when-downgrade
-
回复标签
错误请求
启用 HTTPS
-
标题标签
请求网址:https://example.com/Customer/SaveCustomer
请求方法:POST
状态码:400
远程地址:xxxx.xxxx.xxxx.xxxx:443
推荐人政策:no-referrer-when-downgrade
-
回复标签
错误请求
http和https的区别在于http返回的错误信息带有状态码,而https只返回状态码。
有什么想法吗?
【问题讨论】:
-
如果您在 Chrome 开发者工具(网络选项卡)中查看 Web 响应,返回的 http 状态和负载是什么? HTTPS?请将两者都添加到您的问题中(不是作为 cmets)。
-
原来它与 HTTP 2.0 有关。我关闭了 HTTP 2.0 并切换到了 HTTP 1.1。现在错误消息按预期显示。我不知道为什么 HTTP 2.0 会导致这种行为。
标签: c# jquery asp.net-mvc https bad-request