Javascript fetch()向服务器发送错误的URL请求
我遇到了一个奇怪的问题,使用fetch()执行POST请求。这应该很容易,但我一直有一个来自服务器的错误代码405。此外,请求URL应仅为“http://localhost:3000/api/teddies/order“,但本地Visual_liveserver不知何故一直在URL请求前添加(本地服务器托管在该服务器上,端口为5500:)。。。在下图中,您可以看到错误代码405和这个奇怪的请求URL 通过此链接,您将能够访问。不要犹豫,看看它;)调用这些函数的文件称为“pageFillingPanier.js” 但简言之,请在下面查找包含fetch的函数的代码:Javascript fetch()向服务器发送错误的URL请求,javascript,fetch,Javascript,Fetch,我遇到了一个奇怪的问题,使用fetch()执行POST请求。这应该很容易,但我一直有一个来自服务器的错误代码405。此外,请求URL应仅为“http://localhost:3000/api/teddies/order“,但本地Visual_liveserver不知何故一直在URL请求前添加(本地服务器托管在该服务器上,端口为5500:)。。。在下图中,您可以看到错误代码405和这个奇怪的请求URL 通过此链接,您将能够访问。不要犹豫,看看它;)调用这些函数的文件称为“pageFillingP
const sendPurchaseRequest = async function (dataToSend) {
console.log(dataToSend);
try {
let response = await fetch('http://localhost:3000/api/teddies/order', {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(dataToSend)
});
console.log(response.ok); //it shows false...
let responseData = await response.json();
sessionStorage.setItem('memoryResponse', responseData.orderId);
//window.location = 'remerciement.html';
} catch (error){
console.log(error);
}
}
在下面找到调用该函数的代码:
document.getElementById('bttFormSend').addEventListener('click', function (e) {
e.preventDefault();
let formPurchaseOrder = {
contact : {
firstName : document.getElementById('firstName').value,
lastName : document.getElementById('lastName').value,
email : document.getElementById('email').value,
address : document.getElementById('adress').value,
city : document.getElementById('city').value},
products : []
};
for (let index = 0; index < basketToDisplay.length; index++) {
formPurchaseOrder.products.push(basketToDisplay[index].id);
}
//this function send the POST request to the server
sendPurchaseRequest (formPurchaseOrder);
});
document.getElementById('bttFormSend')。addEventListener('click',函数(e){
e、 预防默认值();
让formPurchaseOrder={
联系人:{
firstName:document.getElementById('firstName')。值,
lastName:document.getElementById('lastName')。值,
电子邮件:document.getElementById('email')。值,
地址:document.getElementById('address')。值,
城市:document.getElementById('city').value},
产品:[]
};
for(让index=0;index
如图所示,url中存在一个不可见的unicode字符。当您从其他地方复制和粘贴url时,有时会发生这种情况
%E2%80%8B
以url编码的形式和代码>无编码
删除它,浏览器会将其识别为有效的url
console.log(encodeURIComponent('http://localhost:3000/api/teddies/order)
非常感谢萨加尔,你让一个男人既高兴又烦恼。。。因为这真是个糟糕的错误。请原谅我的法语。。。作为一个法国人,我被原谅了;)