反应连接到Node Cors预检失败

编程入门 行业动态 更新时间:2024-10-26 10:31:14
本文介绍了反应连接到Node Cors预检失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 var app = express(); app.use(function(req, res, next) { res.header('Access-Control-Allow-Credentials', true); res.header('Access-Control-Allow-Origin', req.headers.origin); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept'); if ('OPTIONS' == req.method) { res.send(200); } else { next(); } });

反应JS提取

React JS Fetch

function request(url, options) { return fetch(url, options) .then(checkStatus) .then(parseJSON); } export function* login() { const username = yield select(makeSelectUsername()); const password = yield select(makeSelectPassword()); const requestURL = 'myurlhere:1337/user/login/'; const requestOptions = { method: 'POST', mode: 'cors', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify( {username: username, password: password} ) } try { const result = yield call(request, requestURL, requestOptions); yield put(loginApiCallSuccess(result)); } catch (err) { yield put(loginApiCallFailure(err)); } }

我需要帮助来确定我的请求出了什么问题或服务器端处理,其中chrome在预检失败后取消了我的请求,也就是为什么我的预检请求失败了。

I need help determining what is wrong with my request or server side handling where chrome is cancelling my requests after preflight failure, aka why is my preflight request failing.

我在localhost上运行react并使用以下命令连接到远程服务器不同的网址。

I am running the react on localhost and connecting to a remote server with a different url.

根据我的判断,预检请求失败,然后chrome取消了该请求。但是,当我在本地运行请求时,登录仍然成功地到达了节点侧的登录路由。

From what I can tell the preflight request is failing then chrome is cancelling the request. However when I ran the request locally the login was still hitting the login route on the node side successfully.

单步执行代码后,在预检调用之后的实际调用中,服务出现400错误。如果我拨打电话但不接听电话,则在开发工具的网络标签中会获得(已取消)。但是,每次在服务器端调用都成功。

When stepping through the code I get a 400 error from the services on the actual call after the preflight call. If I run the call and don't step through I get a (canceled) in the network tab of dev tools. However the call is successful on the server side each time.

General Request URL:jenkins.murmillosoftware:1337/user/register/ Request Method:POST Status Code:400 Bad Request Remote Address:52.5.222.29:1337 Referrer Policy:no-referrer-when-downgrade Response Headers view source Access-Control-Allow-Credentials:true Access-Control-Allow-Headers:X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Origin Access-Control-Allow-Methods:GET,PUT,POST,DELETE Access-Control-Allow-Origin:127.0.0.1:3000 Connection:keep-alive Content-Length:47 Content-Type:application/json; charset=utf-8 Date:Wed, 16 Aug 2017 22:24:48 GMT ETag:W/"2f-vn6Bxm14Gkpb5HFTCsgU2h3Nq3o" set-cookie:connect.sid=s%3AjPVtqIoi6zy0QPmhfFkprFObfwj_J-Lw.sPvW3qRc1Vwj4R6qFBtW0oXykF68Qn%2FAwmLCWrg51qc; Path=/; HttpOnly X-Powered-By:Express Request Headers view source Accept:application/json Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.8 Cache-Control:no-cache Connection:keep-alive Content-Length:44 Content-Type:application/json Host:jenkins.murmillosoftware:1337 Origin:127.0.0.1:3000 Pragma:no-cache Referer:127.0.0.1:3000/login? User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36 Request Payload view source {username: "fdasfas", password: "asdfasdf"} password : "asdfasdf" username : "fdasfas"

推荐答案

您是否尝试过Express / Cors我最近在访问允许来源方面也遇到了问题,然后我从

have you ever tried express/cors i recently also had problem with access allow origin then i download express/cors from

npm install cors

然后在程序中添加

var express = require('express') var cors = require('cors') var app = express() app.use(cors()) app.get('/products/:id', function (req, res, next) { res.json({msg: 'This is CORS-enabled for all origins!'}) }) app.listen(80, function () { console.log('CORS-enabled web server listening on port 80') })

您还可以在[link] https:// github中看到其他使用cors的方法 / expressjs / cors

you can also see other method to use cors in [link]github/expressjs/cors

更多推荐

反应连接到Node Cors预检失败

本文发布于:2023-10-29 09:38:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   Node   Cors

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!