我有一個運行在端口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