我有一个运行在端口3000上的Node.js应用程序,该应用程序正在为它的服务器端Ajax调用使用axios。Node.js用于本地服务器端脚本调用的Axios
据工作如下
我Axio上Ajax调用在/public/views/example.js
example() {
axios.get (
// server ip, port and route
"http://192.168.1.5:3000/example", {
params : {
arg01: "nothing"
}
}
)
.then (
result => console.log(result)
)
.catch (
error => console.log(error)
);
}
,并呼吁/公/逻辑/ example_route路线作出.js文件
router.get("/example", function(req, res) {
// just to test the ajax request and response
var result = req.query.arg01;
res.send(result);
});
因此,这是所有工作正常,当我从网络内部运行但如果我尝试从网络外部运行它(使用具有3000端口转发的DNS),它会失败,我想这是因为当外部执行时192.168.1.5不再有效,因为我必须使用DNS。
当我改变Axios公司调用以下
example() {
axios.get (
// server ip, port and route
"http://www.dnsname.com:3000/example", {
params : {
arg01: "nothing"
}
}
)
.then (
result => console.log(result)
)
.catch (
error => console.log(error)
);
}
然后再从外部而不是内部运作。有没有解决这个问题的方法?
我知道用PHP,让AJAX调用的时候,我没有这个问题,因为我可以使用脚本的实际位置,而不是一个路线
$.ajax({
url : "logic/example.php",
type : "GET",
dataType : "json",
data : {
"arg01":"nothing"
},
success : function(result) {
console.log(result);
},
error : function(log) {
console.log(log.message);
}
});
是有可能实现与节点类似的东西.js和axios?
您是否尝试过使用'/ example'作为url而不是'http://www.dnsname.com:3000/example'? – Molda
哇这工作我不知道这是如此聪明,你会发布一个答案,所以我可以接受它是正确的 – Trent
很酷。添加了答案。谢谢 – Molda