尽管我已经学习并在我的本地服务器上使用Node实现了AJAX请求。我发现我在本地服务器上创建的请求在云服务器上不起作用。
当我使用AJAX向服务器(Node)请求数据时,由于URL的原因,请求无法到达服务器(或者我认为是这样)。
节点编码:
app.get("/",function(req,res){
app.use(express.static(__dirname + '/'));
app.get("/mypath/",function(req,res){
console.log("please tell me that you arrive here"); //This actually never happens
//some functions using the data in "req" and putting there in var "data"
res.send(data);
});
});Javascript代码:
$.ajax({
url: 'http://localhost:3000/mypath/',
type: 'GET',
data: {//some data to use on the server},
dataType: "json",
complete: function(data){
//some stuff with the transformed data
},
});上面的代码可以在本地服务器(我的pc)上运行。但不是在云端,我在尝试使用NGINX和Express处理服务器静态文件时遇到了一些问题,但我能够解决这个问题。
您是否认为,考虑到我提供静态文件的方式以及我在云服务器上工作的方式,当我们试图通过URL进行通信时,我应该以不同的方式使用AJAX请求吗?
控制台日志:
Console Log after using Marcos solution
编辑:来自jQuery AJAX和节点请求的代码
节点设置:
var express = require('express');
var app = express();
var request = require("request");
var db = require('mysql');
const path = require('path');
var http = require("http").Server(app);
var io = require("/usr/local/lib/node_modules/socket.io").listen(http);
http.listen(3000, 'localhost'); //At the end of everything between here and the code.获取节点代码:
app.use(express.static(__dirname + '/'));
app.get('/reqData',function(req,res){
//I never arrive here
console.log("print that I am here");
transform(req.query.selection1,req.query.selection2,function(){
res.json(data); //data is transformed globally
});
});Ajax:
function requestData(){
$.ajax({
url: 'http://localhost:3000/reqData',
type: 'GET',
data: {//Some stuff to send},
dataType: "json",
complete: function(data){
do(data);
},
error: function(e){
console.log(e);
}
});
}新控制台日志: Chrome Console Error
https://stackoverflow.com/questions/50615248
复制相似问题