Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在get请求后重定向到页面_Javascript_Jquery_Html_Node.js_Ajax - Fatal编程技术网

Javascript 在get请求后重定向到页面

Javascript 在get请求后重定向到页面,javascript,jquery,html,node.js,ajax,Javascript,Jquery,Html,Node.js,Ajax,我正在通过两个元素创建一个网页生成器:ejs格式的前端和nodejs格式的后端。在前端,我有一个带有按钮的文本框,用来从用户那里获取一些输入。按下按钮时,前端执行对服务器的GET请求(使用服务器节点到外部url),我的目标是使用从GET请求检索的数据创建一个表。实际上,我的代码执行对服务器的GET请求,但我无法将我的ejs页面重定向到新页面(由表组成) 这是我的密码: index.ejs <html lang="en"> <%- include header %> &l

我正在通过两个元素创建一个网页生成器:ejs格式的前端和nodejs格式的后端。在前端,我有一个带有按钮的文本框,用来从用户那里获取一些输入。按下按钮时,前端执行对服务器的GET请求(使用服务器节点到外部url),我的目标是使用从GET请求检索的数据创建一个表。实际上,我的代码执行对服务器的GET请求,但我无法将我的ejs页面重定向到新页面(由表组成) 这是我的密码: index.ejs

<html lang="en">

<%- include header %>
<!-- Main component for a primary marketing message or call to action -->
<div class="jumbotron">
<h1>Welcome</h1>
<p>Hello Dashboard</p>
</div>
</html>

<div class="row">
<form class="col s12" id="plate">
    <div class="row">
        <div class="input-field col s12">
            <label for="plate">Insert plate</label>
            <input type="text" id="myText" >
              <button class="btn waves-effect waves-light" type="submit" onclick="search()">Submit
               <i class="material-icons right">send</i>
              </button>
        </div>
    </div>
</form>
</div>
<script>
function search() {
var x = document.getElementById("myText").value;
var url="http://localhost:1500/block?plate="+x;
$.get(url, function(data, status){
      window.location.replace(url);
});
}
</script>
这是my/routes/index.js:

exports.block = function(req, res, next){
const request = require('request');
plate= req.query.plate;
 url="http://150.145.11.134:3000/api/queries/GetSostaByPlate? 
request_platenumber="+plate;
request(url, function (err, response, body) {
if (err || response.statusCode !== 200) {
  return res.sendStatus(500);
}
console.log(JSON.parse(body));
res.render('plate.ejs', { results : JSON.parse(body) });

});
};
我的目标是将页面重定向(按下按钮后)到类似
http://localhost:1500/block?plate=

谢谢你的帮助

expressapp.get('/block', function(req, res) {
  res.render('plate.ejs', { results : JSON.parse(body) });
});   

expressapp.get('/', function(req, res) {
  if (user.block) {
     res.redirect('/block');
  } else {
    res.render('index')
  }
});
基本上,您定义了
/block
应该如何呈现,然后将目标页面重定向到
/block
,并且它应该适当地呈现

expressapp.get('/block', function(req, res) {
  res.render('plate.ejs', { results : JSON.parse(body) });
});   

expressapp.get('/', function(req, res) {
  if (user.block) {
     res.redirect('/block');
  } else {
    res.render('index')
  }
});