[英]Node Express pass variable from client to server
I am trying to return value from an input box to a variable in route, by taking this source as a reference but am stuck, in the example author is searching a keyword, constructing a url using this keyword, and returning a body response. 我试图将输入源的值返回到路由中的变量,方法是以该源作为参考,但被困在示例中,作者正在搜索一个关键字,使用该关键字构造一个url,并返回一个正文响应。
My use case is a little different I need user to provide a url as a string in search box, which I would then pass to my request function to spit response body 我的用例有些不同,我需要用户在搜索框中提供一个URL作为字符串,然后将其传递给我的请求函数以吐出响应正文
Current app.js (server-side) 当前的app.js(服务器端)
app.get('/searching', function(req, res){
// input value from search
var url = req.query.search;
console.log(url); // prints value
request({ uri: url}, function (error, response, body) {
if (!error) {
content = body;
console.log(content);
} else {
console.log('Oops! Error when contacting slack.com');
}
});
res.send(content);
});
main.js (client-side) main.js(客户端)
$(function(){
$('#submit').on( 'click', function () {
var sSearch = $('#search').val();
var parameters = { search: sSearch };
$.get( '/searching',parameters, function(data) {
$('#results').html(data);
});
});
});
I realize /searching
in both above files must be replaced because currently its trying to 'search' the user entered url value as a query string, so if I enter " https://google.com " in the text box the application tries to search: 我意识到必须替换以上两个文件中的/searching
search,因为当前它正在尝试“搜索”用户输入的url值作为查询字符串,因此,如果我在文本框中输入“ https://google.com ”,则应用程序将尝试搜索:
http://localhost:3000/searching?search=https%3A%2F%2Fgoogle.com HTTP://本地主机:3000 /搜索查询= HTTPS%3A%2F%2Fgoogle.com
instead I need it to pass this url as is to the request
function so that I can print the body response
, which in this case would be the page's source code. 相反,我需要它按原样将此URL传递给request
函数,以便我可以打印body response
,在这种情况下,它是页面的源代码。 But am not sure how should I proceed 但是我不确定该如何进行
index.jade (template) index.jade(模板)
extends layout
block content
input#search(type="search", placeholder="Enter Keyword")
button#submit(type='submit',class='btn btn-success') Search
h2#results
script(src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js")
script(src="/javascripts/main.js")
How should I format so that I can pass the variable from client to server and then send it back to client after processing? 我应该如何格式化,以便可以将变量从客户端传递到服务器,然后在处理后将其发送回客户端? Any help is appreciated. 任何帮助表示赞赏。
In the app.js
you are making a async http call, but the response is sent before the http callback is invoked. 在app.js
您正在进行异步http调用,但响应是在调用HTTP回调之前发送的。 Just move the res.send(content);
只需移动res.send(content);
inside the call back 在回叫内
app.get('/searching', function(req, res){
// input value from search
var url = req.query.search;
console.log(url); // prints value
request({ uri: url}, function (error, response, body) {
if (!error) {
content = body;
console.log(content);
} else {
console.log('Oops! Error when contacting slack.com');
}
res.send(content);
});
});
i see ajax request in your client code , so in your server code, try to response something like this : 我在您的客户端代码中看到ajax请求,因此在您的服务器代码中,尝试响应如下内容:
res.status(200).json(content)
in your client code ,check if the content is there (console.log(data) before $().html(data)... ) 在您的客户端代码中,检查内容是否存在($()。html(data)...之前的console.log(data))
PD : request is async , so you have to response inside request callback. PD:request是async,因此您必须在request回调内进行响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.