简体   繁体   English

在JS中获取400响应错误消息

[英]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.

相关问题 响应 Post 请求时出现 400 错误 - Getting 400 error in response on Post request Django / JS:Ajax响应未收到消息 - Django / JS: Ajax response is not getting message Json收到错误400 - Json getting error 400 我在 react.js 中收到 400 响应错误我知道后端没问题但客户端我不确定我不熟悉反应 - i`m getting the 400 response error in react.js i know the backendd is ok but the client side i am not sure i am not fimiliar with react 使用koa.js + axios获取错误消息 - Getting error message with koa.js + axios 为什么我的 GraphQL 查询会收到类型错误的 400 响应? - Why am I getting a 400 response with a type error for my GraphQL query? 收到错误消息“未找到JavaScript支持”,而不是响应 - Getting error message “No javascript support found” instead of the response Laravel:从控制器获取错误响应但在控制台上输入错误并且响应消息未显示在视图页面上 - Laravel: Getting error response from controller but type error on console and response message is not showing on view page AJAX发布方法中的400错误响应错误 - 400 Bad Response Error in AJAX Post Method 尝试从node.js发出POST请求时遇到400 Request TimeOut错误 - Getting 400 Request TimeOut error on trying to do a POST request from node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM