加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

nodejs-express接收ajax请求

发布时间:2020-12-16 00:26:29 所属栏目:百科 来源:网络整理
导读:index.ejs !DOCTYPE htmlhtml 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: {

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);
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读