![](/img/trans.png)
[英]how to send value as a parameter to loadURL() of electron main.js file from node js file?
[英]How do I send and receive the ajax request instead of grabbing the code from the main.js file?
我正在嘗試使用服務器端 Node JS 和客戶端 Jquery 執行 AJAX 請求。
目前我的客戶端代碼如下所示:
let theSearchRequest = $("#search").val();
$.ajax({
method:"GET",
url:"./main.js",
data: theSearchRequest
})
.done( (theListOfNames) => {
$("#listOfNames").html(theListOfNames);
});
我的服務器端代碼如下所示:
let request = require("request");
let names = ["ryan", "liam", "john", "james", "michael"];
request.get({
url:"./index.html",
},
(error, response, body) =>{
console.log(body);
});
輸出是 ID 為“listOfNames”的“p”標記顯示服務器端代碼。
如何發送和接收 ajax 請求而不是從 main.js 文件中抓取代碼?
您必須在任何特定鍵中傳遞搜索輸入
let theSearchRequest = $("#search").val();
$.ajax({
method:"GET",
url:"./main.js",
data: {search: theSearchRequest}
})
.done( (theListOfNames) => {
$("#listOfNames").html(theListOfNames);
});
在服務器端,您將在查詢字符串參數中獲得搜索輸入。
let request = require("request");
let names = ["ryan", "liam", "john", "james", "michael"];
request.get({
url:"./index.html",
},
(error, response, body) =>{
console.log(body.query);
});
在您的 jQuery ajax 請求中,您正在調用url:"./main.js"
這實際上是一個文件(一個 javascript 文件)。
我假設main.js
是你的服務器端代碼,對吧? 您想要做的是在客戶端的 ajax 請求中不調用main.js
而是調用index.html
。
最重要的是,您應該將 searchRequest 值轉換為查詢字符串並將其發送到服務器。
let theSearchRequest = $("#search").val();
//e.g.
let theSearchRequestQueryString = 'searchRequest=' + theSearchRequest;
$.ajax({
method:"GET",
url:"./main.js",
data: theSearchRequestQueryString
})
.done( (theListOfNames) => {
$("#listOfNames").html(theListOfNames);
});
我不知道這是否是您真正想要實現的(調用index.html
文件)。
無論如何,為了正常工作,需要運行 node.js 服務器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.