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

相关文章

$.AJAX()方法中的PROCESSDATA参数 在使用jQuery的$.ajax()方...
form表单提交的几种方式 表单提交方式一:直接利用form表单提...
文章浏览阅读1.3k次。AJAX的无刷新机制使得在注册系统中对于...
文章浏览阅读1.2k次。 本文将解释如何使用AJAX和JSON分析器在...
文章浏览阅读2.2k次。/************************** 创建XML...
文章浏览阅读3.7k次。在ajax应用中,通常一个页面要同时发送...