简体   繁体   English

AJAX 对节点 JS 服务器的 POST 请求无法从 IPAD 或 IPHONE 工作

[英]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:条件:

  • Firewall on Windows 10 OS configured to allow incoming data on port 80 and 443 (Apache) and 8000 (Express). Windows 10 操作系统上的防火墙配置为允许端口 80 和 443 (Apache) 和 8000 (Express) 上的传入数据。
  • On the IPAD and IPHONE cookies deleted and "Do not allow following" unchecked.在 IPAD 和 IPHONE cookies 上删除并且“不允许关注”未选中。
  • All on the same LAN.都在同一个局域网上。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM