asp.net core Ajax 请求给出 400 bad request 但 POSTMAN 可以工作

发布于 2025-01-09 02:41:24 字数 2638 浏览 1 评论 0原文

我在 asp.net core blazor 应用程序中遇到 ajax 请求的问题。我已经尝试了几乎所有我能在 stackoverflow 上找到的与 ajax post 调用结果相关的 400 错误请求 我有以下控制器

[Route("api/{controller}/{action}/{id?}")]
    public class BoldReportsAPIController : ControllerBase, IReportController
    {
        // Report viewer requires a memory cache to store the information of consecutive client request and
        // have the rendered report viewer information in server.
        private IMemoryCache _cache;

        // IHostingEnvironment used with sample to get the application data from wwwroot.
        private IWebHostEnvironment _hostingEnvironment;

        public BoldReportsAPIController(IMemoryCache memoryCache, IWebHostEnvironment hostingEnvironment)
        {
            _cache = memoryCache;
            _hostingEnvironment = hostingEnvironment;
        }
// Post action to process the report from server based json parameters and send the result back to the client.
        [HttpPost]
        public object PostReportAction([FromBody] Dictionary<string, object> jsonArray)
        {
            return ReportHelper.ProcessReport(jsonArray, this, this._cache);
        }

       
    }

当我向邮递员发出请求时,它工作正常,如下所示。 邮递员呼叫

但是当我进行 ajax 调用时,它会给出 400 bad request 错误。

我已经用邮递员生成的代码替换了原始的 ajax 调用,但该代码也不起作用。

 var settings = {
                    "async": true,
                    "crossDomain": true,
                    "url": "https://localhost:44313/api/BoldReportsAPI/PostReportAction",
                    "method": "POST",
                    "headers": {
                        "content-type": "application/json",
                        "cache-control": "no-cache",
                        "postman-token": "fbed680d-0143-ab86-24e6-176c16d713bf"
                    },
                    "processData": false,
                    "data": "{\"reportAction\":\"ReportLoad\",\"isReloadReport\":false,\"controlId\":\"report-viewer\",\"reportPath\":\"sales-order-detail\",\"enableVirtualEvaluation\":false,\"reportServerUrl\":\"\",\"processingMode\":\"remote\",\"locale\":\"en-US\",\"accessInternalValue\":false,\"customBrandSettings\":{\"hideHelpLink\":false,\"customDomain\":\"https://help.boldreports.com\",\"customBrandName\":\"Bold Reports\",\"customLinks\":[{\"name\":\"ESLicenseMessage\",\"url\":\"/licensing/license-token/\"}]}}\r\n"
                }

                $.ajax(settings).done(function (response) {
                    console.log(response);
                });

I am facing trouble with ajax request in asp.net core blazor application. I have tried almost everything i can find on stackoverflow related to ajax post call results in 400 bad request
I have following controller

[Route("api/{controller}/{action}/{id?}")]
    public class BoldReportsAPIController : ControllerBase, IReportController
    {
        // Report viewer requires a memory cache to store the information of consecutive client request and
        // have the rendered report viewer information in server.
        private IMemoryCache _cache;

        // IHostingEnvironment used with sample to get the application data from wwwroot.
        private IWebHostEnvironment _hostingEnvironment;

        public BoldReportsAPIController(IMemoryCache memoryCache, IWebHostEnvironment hostingEnvironment)
        {
            _cache = memoryCache;
            _hostingEnvironment = hostingEnvironment;
        }
// Post action to process the report from server based json parameters and send the result back to the client.
        [HttpPost]
        public object PostReportAction([FromBody] Dictionary<string, object> jsonArray)
        {
            return ReportHelper.ProcessReport(jsonArray, this, this._cache);
        }

       
    }

When i make request with postman it works fine as shown below.
Postman call

But when i make ajax call it gives 400 bad request error.

I have literally replaced original ajax call with the code generated from postman but that code doesn't work also.

 var settings = {
                    "async": true,
                    "crossDomain": true,
                    "url": "https://localhost:44313/api/BoldReportsAPI/PostReportAction",
                    "method": "POST",
                    "headers": {
                        "content-type": "application/json",
                        "cache-control": "no-cache",
                        "postman-token": "fbed680d-0143-ab86-24e6-176c16d713bf"
                    },
                    "processData": false,
                    "data": "{\"reportAction\":\"ReportLoad\",\"isReloadReport\":false,\"controlId\":\"report-viewer\",\"reportPath\":\"sales-order-detail\",\"enableVirtualEvaluation\":false,\"reportServerUrl\":\"\",\"processingMode\":\"remote\",\"locale\":\"en-US\",\"accessInternalValue\":false,\"customBrandSettings\":{\"hideHelpLink\":false,\"customDomain\":\"https://help.boldreports.com\",\"customBrandName\":\"Bold Reports\",\"customLinks\":[{\"name\":\"ESLicenseMessage\",\"url\":\"/licensing/license-token/\"}]}}\r\n"
                }

                $.ajax(settings).done(function (response) {
                    console.log(response);
                });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜血缘 2025-01-16 02:41:24

这是由于蚂蚁伪造令牌造成的。显然,ABP 会自动添加防伪令牌,因此在添加

[IgnoreAntiforgeryToken(Order = 2000)]

到我的操作方法后,问题就得到了解决。

但感觉就像我破坏了安全。

It was due to ant forgery token. Apparently, ABP automatically adds antiforgery token so after adding

[IgnoreAntiforgeryToken(Order = 2000)]

to my action method, issue was resolved.

But it feels like I am breaking security.

自由如风 2025-01-16 02:41:24

使用 ValidateAntiForgeryToken 时,您必须添加报告查看器 API 交互的标头。您可以参考以下文章来添加报表查看器交互的标题。这些需要使用 ReportViewer introp 添加。

https://help.boldreports.com/embedded-reporting/javascript-reporting/report-viewer/handle-post-actions/#add-custom-header-in-ajax-request

While using ValidateAntiForgeryToken, you have to add the headers for the Report Viewer API interaction. You can refer to the below article to add the headers for your Report Viewer interaction. These need to be added with the ReportViewer introp.

https://help.boldreports.com/embedded-reporting/javascript-reporting/report-viewer/handle-post-actions/#add-custom-header-in-ajax-request

七婞 2025-01-16 02:41:24

就我而言,我将不带引号的数字作为字符串化 json 对象值传递。我已经找到并修复了很多问题,直到我开始在 dotnet 8 中托管我的 api。现在看来序列化有点严格了。

In my case I was passing unquoted numbers as stringified json object values. I had found and fixed many and gotten away with it until I started hosting my api in dotnet 8. It appears serialization is a bit stricter now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文