![](/img/trans.png)
[英]Node.js javascript - call function from external script server-side
[英]Node.js Axios for local Server Side script call
我有一個在端口3000上運行的Node.js應用程序,它使用axios進行服務器端的ajax調用。
它的工作原理如下
我的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)
);
}
以及它調用的路線/public/logic/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);
}
});
是否有可能實現與Node.js和axios類似的東西?
您可以在沒有actuall主機和端口的情況下使用該路徑。
example() {
axios.get (
// just the path without host or port
"/example", {
params : {
arg01: "nothing"
}
}
)
.then (
result => console.log(result)
)
.catch (
error => console.log(error)
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.