微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nodejs-express接收ajax请求

index.ejs

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
    <script src="/javascripts/jquery.min.js"></script>
    <script>

        function ajax(){
            $.ajax({
                url: "/ajax",type: "GET",dataType: "json",data: {
                    id: 111,title: 'aaa',content: 'bbb'
                },contentType: "application/json",cache: false,timeout: 5000,complete: function() {
                    //called when complete
                    console.log('process complete');
                },success: function(data) {

                    console.log(data);
                    console.log('process sucess');
                    alert(data);
                },error: function() {
                    console.log('process error');
                }
            });
        }
    </script>
  </head>
  <body>
    <h1><%= title %></h1>
    <p>Welcome to <%= title %></p>
  <button onclick="ajax()">aaaaa</button>
  </body>
</html>
route.ajax
exports.ajax=function(req,res){
    console.log(req.query.id)
    console.log(req.query.title);
    console.log(req.query.content);
    var a={
      id:req.query.id,title:req.query.title
    };
    res.send(a);
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐