第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 XMLHttpRequest 在 Javascript 中發(fā)生 POST 錯(cuò)誤

使用 XMLHttpRequest 在 Javascript 中發(fā)生 POST 錯(cuò)誤

慕村225694 2023-06-29 15:42:18
我正在開(kāi)發(fā)一個(gè)需要 API 的項(xiàng)目,更準(zhǔn)確地說(shuō),需要 POST 方法。我讀過(guò)關(guān)于它的堆棧溢出線程,但它沒(méi)有多大幫助:他們只是說(shuō)使用Access-Control-Allow-Origin盡管我已經(jīng)這樣做了。所以,這是前端的一面:const CHEMIN_AU_SERVEUR = "http://localhost:3000/api/stuff";const func_that_post_the_card_created = (path, json) => {    const request_projets_post = new XMLHttpRequest();    request_projets_post.open("POST", path, true);    request_projets_post.setRequestHeader("Content-type", "application/x-www-form-urlencoded");    request_projets_post.send(JSON.stringify(json));};func_that_post_the_card_created(CHEMIN_AU_SERVEUR, "title=title&description=description&imageUrl=imageUrl&href=href&github_href=github_href");這里是服務(wù)器端server.jsconst http = require('http');const app = require('./app');app.set('port', process.env.PORT || 3000);console.log(process.env.PORT || 3000);const server = http.createServer(app);server.listen(process.env.PORT || 3000);應(yīng)用程序.jsconst express = require("express");const app = express();const bodyParser = require("body-parser");const ProjectScheme = require("./models/Project");const mongoose = require("mongoose");// The mongoose connect that will not showapp.use((req, res, next) => {  res.setHeader("Access-Control-Allow-Origin", "*");  res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content, Accept, Content-Type, Authorization");  res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS");  next();});app.use(bodyParser.json());app.post("/api/stuff", (req, res, next) => {  const projet = new ProjectScheme({ ...req.body });  projet.save()    .then(() => res.status(201).json({ message: "Projet enregistré !" }))    .catch((error) => res.status(400).json({ error }));});謝謝你的幫助。這將會(huì)非常有幫助。我認(rèn)為這也會(huì)幫助很多其他人。
查看完整描述

1 回答

?
大話西游666

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊

問(wèn)題就在這里:


request_projets_post.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

request_projets_post.send(JSON.stringify(json));

在第一行中,您將內(nèi)容類型設(shè)置為application/x-www-form-urlencoded。在第二個(gè)中,您的正文是一個(gè) JSON 字符串。


您發(fā)送到該函數(shù)的數(shù)據(jù)是經(jīng)過(guò) urlencoded 的:


title=title&description=description&imageUrl=imageUrl&href=href&github_href=github_href

但在你的服務(wù)器上,你將正文解析為 json:


app.use(bodyParser.json());

您不能混合編碼。您需要決定是使用 JSON 還是 urlencoded:


JSON

在你的前端:


request_projets_post.setRequestHeader("Content-type", "application/json");

request_projets_post.send(JSON.stringify(json));

您提供給函數(shù)的數(shù)據(jù)是一個(gè)對(duì)象:


func_that_post_the_card_created(CHEMIN_AU_SERVEUR, {

  title: title,

  description: description,

  imageUrl: imageUrl,

  href: href,

  github_href: github_href

});

后臺(tái)無(wú)需修改


URL編碼

不要使用 JSON.stringify:


const func_that_post_the_card_created = (path, data) => {

    const request_projets_post = new XMLHttpRequest();

    request_projets_post.open("POST", path, true);

    request_projets_post.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    request_projets_post.send(data);

};


func_that_post_the_card_created(CHEMIN_AU_SERVEUR, "title=title&description=description&imageUrl=imageUrl&href=href&github_href=github_href")

在您的服務(wù)器中刪除該行


app.use(bodyParser.json());

并添加:


app.use(bodyParser.urlencoded({ extended: true }));


查看完整回答
反對(duì) 回復(fù) 2023-06-29
  • 1 回答
  • 0 關(guān)注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)