gpt4 book ai didi

javascript - koa-cors 和 Access-Control-Allow-Credentials 的问题

转载 作者:数据小太阳 更新时间:2023-10-29 05:14:15 28 4
gpt4 key购买 nike

我有这个错误

XMLHttpRequest cannot load http://127.0.0.1:1337/. Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. Origin 'http://localhost:63342' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

我在这里阅读了一些主题,但还没有找到适合我的解决方案。也许我只是不明白这一切是如何运作的。但是,我该如何解决这个问题?提前谢谢你。

  • server.js

const Koa = require('koa')
const Router = require('koa-router')
const koaBody = require('koa-body')()
const router = new Router({})
const koaCors = require('koa-cors')


router
.post('/', koaBody, async function (ctx) {
console.log(ctx.request.body)
ctx.status = 200
ctx.body = 'POST'
})
.get('/', async function (ctx) {
ctx.status = 200
ctx.body = 'GET'
})

exports.createServer = function () {
const app = new Koa()
app
.use(router.routes())
.use(router.allowedMethods())
.use(koaCors())
app.listen(1337)
}

exports.createServer()

  • index.js

function submitForm(form) {
let xhr = new XMLHttpRequest()
xhr.withCredentials = true;
xhr.open('POST', 'http://127.0.0.1:1337/', true)
xhr.setRequestHeader('Access-Control-Allow-Origin', '*')
let formData = new FormData(form)
let body = {
name: formData.get('name'),
password: formData.get('password'),
message: formData.get('message')
}
xhr.onreadystatechange = function() {
if(xhr.status == 200) {
alert('Hello!')
} else {
alert('Something went wrong')
}
}
xhr.send(JSON.stringify(body))
}

$(document).ready(function () {
$('#form').submit(function (event) {
event.preventDefault();
if (validateForm($form)) {
$('#modal-form').modal('hide');
submitForm($form)
}
return false;
})
});

更新:

我希望修复服务器端。我的 index.js 现在:

function submitForm(form) {
let xhr = new XMLHttpRequest()
xhr.withCredentials = true;
xhr.open('POST', 'http://127.0.0.1:1337/', true)
xhr.setRequestHeader('Access-Control-Allow-Origin', '*')
let formData = new FormData(form)
xhr.onreadystatechange = function() {
if(xhr.status == 200) {
alert('Hello!')
console.log(xhr.response);
} else {
alert('Something went wrong')
}
}
xhr.send(formData)

和 server.js:

router
.post('/', koaBody, function (ctx) {
console.log(ctx.request.body)
ctx.status = 200
ctx.body = 'POST'
})
.get('/', function (ctx) {
ctx.status = 200
ctx.body = 'GET'
});exports.createServer = function () {
const app = new Koa()
const koaOptions = {
origin: true,
credentials: true
};
app
.use(router.routes())
.use(router.allowedMethods())
.use(cors(koaOptions))
app.listen(1337)}

再次 请求的资源上不存在“Access-Control-Allow-Origin” header 。

现在我做错了什么?

最佳答案

The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'.

为防止该问题,您需要设置 koa-cors credentials选项:

exports.createServer = function () {
const app = new Koa()
const koaOptions = {
origin: true,
credentials: true
};
app
.use(router.routes())
.use(router.allowedMethods())
.use(koaCors(koaOptions))
app.listen(1337)
}

关于javascript - koa-cors 和 Access-Control-Allow-Credentials 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46755800/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com