简体   繁体   English

由于 CORS 问题,AJAX POST 未使用 body-parser 向 express.js 服务器发送请求(已解决)

[英]AJAX POST not sending request to express.js server with body-parser due CORS problem (SOLVED)

I'm trying to make an AJAX POST request from my html page which runs on an apache server.我正在尝试从在 apache 服务器上运行的 html 页面发出 AJAX POST 请求。 The request is needed to POST a filename to the express.js server and do some stuff with it with another node.该请求需要将文件名发布到 express.js 服务器并与另一个节点一起使用它做一些事情。 I also installed the body-parser node.我还安装了 body-parser 节点。

What am I missing?我错过了什么? The server works fine and when I test it in the browser it will return some info.服务器工作正常,当我在浏览器中测试它时,它会返回一些信息。 My node express server does not seem to recieve my ajax call.我的节点快递服务器似乎没有收到我的 ajax 呼叫。 I've been reading and trying several posts on this subject on this website for days now.几天来,我一直在阅读并尝试在此网站上发布有关此主题的几篇文章。

my code in the scripts part of my html page我在 html 页面的脚本部分中的代码

 function ImportFIT() { var data = {"fname":"test"}; $.ajax({ url: 'http://localhost:8000/FileName', type: 'POST', contentType: "application/json", data: JSON.stringify(data), success: function(result) { alert('success'); }, error: function (xmlHttpRequest, textStatus, errorThrown) { alert("error"); } }); }

And the code in node.js以及 node.js 中的代码

 var express = require('express'); var path = require('path'); var app = express(); var bodyParser = require('body-parser'); const port = 8000 app.use(bodyParser.json()); app.post('/FileName', function (req, res) { console.log(req.body.fname); }) app.listen(port, () => { console.log(`FIT app listening at http://localhost:${port}`) })

Are you really sure that it isn't a CORS issue?您真的确定这不是 CORS 问题吗? You aren't handling get requests on the express server, so it could possibly be in a different server/file that cannot access the express server.您没有在快速服务器上处理 get 请求,因此它可能位于无法访问快速服务器的不同服务器/文件中。 Inspect with CTRL-SHIFT-I and look in the console if there are any errors.使用CTRL-SHIFT-I检查并查看控制台是否有任何错误。 If there is an error see if it resembles this: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors#identifying_the_issue .如果出现错误,请查看它是否类似于: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors#identifying_the_issue
If so then you can disable CORS on your website by changing the NodeJS code to如果是这样,那么您可以通过将 NodeJS 代码更改为来禁用 CORS

var express = require('express');
var path = require('path');
var app = express();
var cors = require("cors");
var bodyParser = require('body-parser');
const port = 8000

app.use(bodyParser.json());
app.use(cors());
app.post('/FileName', function(req, res) {
  console.log("worked");
  console.log(req.body.fname);
  res.send();
})

app.listen(port, () => {
  console.log(`FIT app listening at http://localhost:${port}`)
})

Install the cors module accordingly.相应地安装cors模块。 If you do not see an error similar to the one in the link posted, you might see an error like "$ is not defined" meaning Jquery isn't loaded on your page, or simply the function holding the AJAX request might not be called.如果您没有看到与发布的链接中的错误类似的错误,您可能会看到类似“$ 未定义”的错误,这意味着您的页面上未加载 Jquery,或者只是持有 ZA34A6659BCEAE779F2818 的 function 请求可能不会被称为.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Express.js + body-parser:POST请求中的空req.body - Express.js + body-parser: empty req.body from POST request Node.js/Express.js 请求正文未定义。 body-parser 已安装并导入 - Node.js/Express.js request body is undefined. body-parser is installed and imported 通过 AJAX (Vanilla JavaScript) 的 POST 请求在 Node.js (express/body-parser/ejs) 上生成空对象 - POST request via AJAX (Vanilla JavaScript) produce empty object on Node.js (express/body-parser/ejs) Express.Router、body-parser 和 post 请求正文 - Express.Router, body-parser and post request body Express JS中的正文解析器 - Body-parser in Express JS Nodejs/Express.js/Body-parser单元测试:如何通过中间件来测试路由功能 - Nodejs / Express.js/ Body-parser Unit Test: How to come through middlewares to test the routing function 错误 405 与 AJAX、Javascript、节点(正文解析器和 CORS)和 XMLHttpRequest - Error 405 with AJAX, Javascript, Node (Body-Parser and CORS), and XMLHttpRequest 你如何使用mongodb native,express和body-parser发布请求并保存数据? - How do you use mongodb native, express and body-parser to post request and save data? 使用Express.js在服务器上发送POST请求 - Send a POST request on the server with Express.js 使用带有 express 的 http-proxy 和 body-parser 时,节点挂起 POST 请求 - Node hangs on POST request when using http-proxy and body-parser with express
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM