[英]Getting 400 Response Error Message in JS
I have a dot net MVC controller that returns a 400 response with an error message when there's obviosuly a bad request, as seen below: 我有一个点网MVC控制器,当请求异常明显时,它会返回400响应并显示一条错误消息,如下所示:
return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "This is a bad request");
This controller is hit through the JQuery Post method 该控制器通过JQuery Post方法命中
$.post({
url: window.location.href,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(payload),
success: function (data) {
window.location.href = data;
},
error: function (jqXHR, textStatus, errorThrown) {
$('.submit-calculate').html(jqXHR.responseText);
debugger;
}};
My issue is that the string I return when returning the HttpStatusCodeResult
string property is inaccessible. 我的问题是返回HttpStatusCodeResult
字符串属性时返回的字符串不可访问。 The jqXHR has a property response text which contains a HTML page for a 400 response, which renders the following on the page: jqXHR具有一个属性响应文本,其中包含用于400响应的HTML页面,该页面在页面上呈现以下内容:
Now the error message is display within this HTML but I want to just access that string ( "This is a bad request"
) without having to pull it out of that response. 现在,该HTML中显示了错误消息,但我只想访问该字符串( "This is a bad request"
),而不必将其从响应中拉出。 Not sure if there's a property that I can't see right now or I need to return something different. 不知道是否存在我现在看不到的属性,或者我需要返回其他内容。
You can use JsonResult
class to return JSON result 您可以使用JsonResult
类返回JSON结果
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return new JsonResult(){
Data = "This is a bad request"
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.