[英]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.