<legend id='aOlP3'><style id='aOlP3'><dir id='aOlP3'><q id='aOlP3'></q></dir></style></legend>
    <bdo id='aOlP3'></bdo><ul id='aOlP3'></ul>

  1. <tfoot id='aOlP3'></tfoot>
  2. <i id='aOlP3'><tr id='aOlP3'><dt id='aOlP3'><q id='aOlP3'><span id='aOlP3'><b id='aOlP3'><form id='aOlP3'><ins id='aOlP3'></ins><ul id='aOlP3'></ul><sub id='aOlP3'></sub></form><legend id='aOlP3'></legend><bdo id='aOlP3'><pre id='aOlP3'><center id='aOlP3'></center></pre></bdo></b><th id='aOlP3'></th></span></q></dt></tr></i><div id='aOlP3'><tfoot id='aOlP3'></tfoot><dl id='aOlP3'><fieldset id='aOlP3'></fieldset></dl></div>
  3. <small id='aOlP3'></small><noframes id='aOlP3'>

    1. 我不能使用 json 使用 react 向我的 web api 发出 Post 请求

      I can#39;t use json to make a Post request to my web api using react(我不能使用 json 使用 react 向我的 web api 发出 Post 请求)

            1. <small id='u4yOO'></small><noframes id='u4yOO'>

              • <bdo id='u4yOO'></bdo><ul id='u4yOO'></ul>
              • <tfoot id='u4yOO'></tfoot>

                <legend id='u4yOO'><style id='u4yOO'><dir id='u4yOO'><q id='u4yOO'></q></dir></style></legend>
                  <tbody id='u4yOO'></tbody>

              • <i id='u4yOO'><tr id='u4yOO'><dt id='u4yOO'><q id='u4yOO'><span id='u4yOO'><b id='u4yOO'><form id='u4yOO'><ins id='u4yOO'></ins><ul id='u4yOO'></ul><sub id='u4yOO'></sub></form><legend id='u4yOO'></legend><bdo id='u4yOO'><pre id='u4yOO'><center id='u4yOO'></center></pre></bdo></b><th id='u4yOO'></th></span></q></dt></tr></i><div id='u4yOO'><tfoot id='u4yOO'></tfoot><dl id='u4yOO'><fieldset id='u4yOO'></fieldset></dl></div>
                本文介绍了我不能使用 json 使用 react 向我的 web api 发出 Post 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在 ASP.NET Core 中创建了一个 webapi,我需要使用 React 来使用它,web api 可以正常工作,如果我使用 curl 或 postman 等,它可以正常工作.当我打算使用 React 时,当我尝试使用问题中的 js 对我的 API 发出任何请求时,问题就开始了.

                I created a webapi in ASP.NET Core, and I need to consume it using React, the web api works normally, if I use curl or postman among others, it works normally. The problem starts when I'm going to use React, when I try to make any requests for my API with js from the problem.

                更复杂的是,当我向其他 API 发出请求时,它正常工作,这让我相信问题出在我的 API 上,但正如我所说,它只与其他 API 一起工作,但反应却没有.我已经尝试了很多方法.

                To complicate matters further, when I make the request for other APIs it works normally, this led me to believe that the problem was in my API, but as I said it works with others only with the react that it does not. I've tried it in many ways.

                API 正在我本地网络上的 IIS 上运行

                The API is running on an IIS on my local network

                $ .ajax ({
                    method: "POST",
                    url: 'http://192.168.0.19:5200/api/token',
                    beforeSend: function (xhr) {
                      xhr.setRequestHeader ("Content-type", "application / json");
                    },
                    date: {
                      name: 'name',
                      password: 'password'
                    },
                    success: function (message) {
                        console.log (message);
                    },
                    error: function (error) {
                        / * if (error.responseJSON.modelState)
                            showValidationMessages (error.responseJSON.modelState); * /
                            console.log (error);
                    }
                  });
                

                使用提取

                const headers = new Headers ();
                    headers.append ('Content-Type', 'application / json');
                
                    const options = {
                      method: 'POST',
                      headers,
                      body: JSON.stringify (login),
                      mode: 'cors' // I tried with cors and no-cors
                    }
                
                    const request = new Request ('http://192.168.0.19:5200/api/token', options);
                    const response = await fetch (request);
                    const status = await response.status;
                    console.log (response); * /
                    // POST adds a random id to the object sent
                    fetch ('http://192.168.0.19:5200/api/token', {
                    method: 'POST',
                    body: JSON.stringify ({
                      name: 'name',
                      password: 'password'
                     }),
                    headers: {
                      "Content-type": "application / json; charset = UTF-8"
                    },
                    credentials: 'same-origin'
                    })
                  .then (response => response.json ())
                  .then (json => console.log (json))
                

                使用请求

                var request = new XMLHttpRequest ();
                    request.open ('POST', 'http://192.168.0.19:5200/api/token', true);
                    request.setRequestHeader ('Content-Type', 'application / json; charset = UTF-8');
                    request.send (login);
                

                错误

                控制台

                网络标签

                当我在不将内容类型更改为 JSON 的情况下执行此操作时,它可以工作因为 API 返回说它不是有效类型.

                When I do this without being change the content type to JSON it works because the API returns saying that it is not a valid type.

                推荐答案

                除了在你的 .NET 配置中允许 CORS.您还需要为所有 OPTION 请求返回 200 OK.

                Apart from allowing CORS in you .NET configuration. You also need to return 200 OK for all OPTION requests.

                不知道它是如何在 .NET 中完成的,但只是创建一个检测请求的 METHOD 的中间件,如果是 OPTIONS,则以 200 状态完成请求.

                Not sure how it's done in .NET but just create a middleware that detects the METHOD of the request, and if it's OPTIONS, the finish the request right there with 200 status.

                这篇关于我不能使用 json 使用 react 向我的 web api 发出 Post 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                can#39;t get response status code with JavaScript fetch(无法使用 JavaScript 获取响应状态代码)
                Difference between fetch, ajax, and xhr(fetch、ajax 和 xhr 之间的区别)
                Can I call the Twitter API in client using Fetch?(我可以使用 Fetch 在客户端调用 Twitter API 吗?)
                How to finish all fetch before executing next function in React?(如何在 React 中执行下一个函数之前完成所有获取?)
                TypeError: Failed to execute #39;fetch#39; on #39;Window#39;: Invalid value(TypeError:无法在“窗口上执行“获取:无效值)
                  <tfoot id='wG0Hs'></tfoot>

                  <legend id='wG0Hs'><style id='wG0Hs'><dir id='wG0Hs'><q id='wG0Hs'></q></dir></style></legend>
                    <bdo id='wG0Hs'></bdo><ul id='wG0Hs'></ul>

                      <tbody id='wG0Hs'></tbody>

                      <i id='wG0Hs'><tr id='wG0Hs'><dt id='wG0Hs'><q id='wG0Hs'><span id='wG0Hs'><b id='wG0Hs'><form id='wG0Hs'><ins id='wG0Hs'></ins><ul id='wG0Hs'></ul><sub id='wG0Hs'></sub></form><legend id='wG0Hs'></legend><bdo id='wG0Hs'><pre id='wG0Hs'><center id='wG0Hs'></center></pre></bdo></b><th id='wG0Hs'></th></span></q></dt></tr></i><div id='wG0Hs'><tfoot id='wG0Hs'></tfoot><dl id='wG0Hs'><fieldset id='wG0Hs'></fieldset></dl></div>

                      <small id='wG0Hs'></small><noframes id='wG0Hs'>