[英]AJAX POST request to node JS server not working from IPAD or IPHONE
I have an Apache server running with a website on Windows 10 OS.我有一个 Apache 服务器在 Windows 10 操作系统上运行一个网站。 On button click on the website I make an AJAX POST request to an Express server in Node JS to run some code and return a JSON string.
在按钮上单击网站,我向 Node JS 中的 Express 服务器发出 AJAX POST 请求,以运行一些代码并返回 JSON 字符串。 This works fine on my Windows 10 OS.
这在我的 Windows 10 操作系统上运行良好。
When I click the button on the website on my IPAD or IPHONE nothing happens.当我单击 IPAD 或 IPHONE 上网站上的按钮时,没有任何反应。 In the command prompt I see no console log of anything reaching the Express server.
在命令提示符下,我看不到任何到达 Express 服务器的控制台日志。
Conditions:条件:
What am I missing here?我在这里想念什么? (next, some code) Express Server JS
(接下来,一些代码) Express Server JS
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) { var Fname = req.body.fname; console.log("received filename: " + Fname); // Require the module var EasyFit = require('./dist/easy-fit.js').default; // Read a.FIT file var fs = require('fs'); fs.readFile('C:/xampp/htdocs/ATP/FIT/' + Fname + '', function (err, content) { // Create a EasyFit instance (options argument is optional) var easyFit = new EasyFit({ force: true, speedUnit: 'km/h', lengthUnit: 'km', temperatureUnit: 'celcius', elapsedRecordField: true, mode: 'list', }); // Parse your file easyFit.parse(content, function (error, data) { // Handle result of parse method if (error) { console.log(error); } else { //res.send(JSON.stringify(data)); res.send(data); console.log('Parse ok'); } }); }); }) app.listen(port, '0.0.0.0'); console.log(`FIT app listening at http://localhost:${port}`);
AJAX Request JS on client side AJAX 在客户端请求 JS
$.ajax({ cache: false, url: 'http://localhost:8000/FileName', type: 'POST', contentType: "application/json", data: JSON.stringify(data), success: function(result) { console.log('received fit in JSON'); console.log(result); });
You can find out your ip address using a command like this:您可以使用如下命令找到您的 ip 地址:
ipconfig|findstr IPv4
Then substitute that for localhost
然后将其替换为
localhost
So you will have a line like所以你会有一条线
url: 'http://192.168.1.131:8000/FileName',
instead of the one that says localhost:8000
而不是说
localhost:8000
After turning off my firewall it worked like a charme.关闭我的防火墙后,它就像一个魅力。 Found the answer in the following post.
在下面的帖子中找到了答案。
https://stackoverflow.com/a/48270227/10440418 https://stackoverflow.com/a/48270227/10440418
Allowed the node.js app to go through my firewall.允许 node.js 应用程序通过我的防火墙访问 go。 It works perfectly!
它完美地工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.