nodejs+Ajax访问路由,返回json数据

以用户登录为例,废话不多说,直接上代码。

//js操作 Ajax请求代码
$("#login-submit").click(function(){
        //do something
        var loginData={};
        loginData.userName=$("#userName").val();
        loginData.userPassword=$("#userPassword").val();
        console.log(loginData);
        $.ajax({
            data: loginData,
            url: ‘/login‘,
            type:"post",
            dataType: ‘JSON‘,
            success: function(data){
                console.log(data);
                location.reload()
            },
            error: function(textStatus){
                console.log(‘error ‘ + textStatus );
            }
        });
    })
//node路由代码 app.post(‘/login‘, function (req, res) {
        //生成密码的 md5 值
        var md5 = crypto.createHash(‘md5‘),
            password =         md5.update(req.body.userPassword).digest(‘hex‘);
        //检查用户是否存在
        User.get(req.body.userName, function (err, user) {
            if (!user) {
                console.log(‘error‘, ‘用户不存在!‘);
                req.flash(‘error‘, ‘用户不存在!‘);
                return res.redirect(‘/login‘);//用户不存在则跳转到登录页
            }
            //检查密码是否一致
            if (user.password != password) {
                console.log(‘error‘, ‘密码错误!‘);
                req.flash(‘error‘, ‘密码错误!‘);
                return res.redirect(‘/login‘);//密码错误则跳转到登录页
            }
            //用户名密码都匹配后,将用户信息存入 session
            req.session.user = user;
            console.log(user);
            req.flash(‘success‘, ‘登陆成功!‘);
            res.json({"result":{message:"success"}});
        });
    });
时间: 2024-08-06 03:42:02

nodejs+Ajax访问路由,返回json数据的相关文章

AJAX MVC 服务器返回Json数据,客户端获取Json数据

<> 控制器 Controller using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Script.Serialization; namespace MvcApplication2.Controllers { public class HomeController : Controller { pu

AJAX MVC server返回Json数据,client获取Json数据

<> 控制器 Controller using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Script.Serialization; namespace MvcApplication2.Controllers { public class HomeController : Controller { pu

ajax https请求返回json数据

怎么获取通过ajax请求的html代码:http://zhidao.baidu.com/link?url=Vk7mfepx1tNOp6DAy7KYY_wcUlKQxwwGpmCmct3akDwyXt5P8IIbzY_bLqABCUDFHFtbZs90jmYG11iN5APMKR-T7Acvix9A_DDcOpJA9lu http://www.iteye.com/problems/84999

如何在Crystal Portlet中正确返回JSON数据给AJAX请求?

当Crystal Portlet中需要采用Ajax请求,并让后台返回Json数据时,如何才能正确.方便的返回Json数据呢? 以下两种方法均可: 方法一:Ajax请求时,采用RenderURL,对应Portlet类中采用ajax(data)方法返回Java对象即可: 方法二:Ajax请求时,采用ResourceURL,对应Portlet类中采用ajax(data,response)方法将Java对象直接输出到Response流中:(推荐使用此方法) 分步指南 方法一: Ajax请求时,url采用

jquery实现ajax,返回json数据

jquery实现ajax可以调用几种方法 我经常用的是$get(url,data,callback,type)方法 其中url是异步请求的页面(可以是.ashx文件),data是参数,callback是回调函数,而type是返回数据的类型.type有xml,html,json,text等. 首先,页面引用jquery.js 在页面写ajax处理的js函数 1 2 3 4 5 6 7 8 9 10 11 12 13 function initMeeting() {             $.ge

ashx文件结合ajax使用(返回json数据)

ashx文件返回json数据: public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string userName = string.Empty; string msg = "{{\"code\":\"{0}\",\"msg\":\"{1}\"}}";

通过jquery的ajax异步请求接收返回json数据

jquery的ajax异步请求接收返回json数据方法设置简单,一个是服务器处理程序是返回json数据,另一种就是ajax发送设置的datatype设置为jsonp格式数据或json格式都可以. 代码示例如下: $('#send').click(function () { $.ajax({ type : "GET", url : "a.php", dataType : "jsonp", success : function (data) { $.

CoAP学习笔记——nodeJS node-coap返回JSON数据包

0 前言 本文说明如何使用node-coap返回JSON数据包.CoAP是专门为物联网系统开发的面向网络的应用层协议栈,CoAP建立在UDP协议之上尽可能减少网络开销,又具有HTTP Restful类型的特性.node-coap使用nodejs实现了coap的客户端和服务器端. [测试环境]--ubuntu/Linux [相关博文] [CoAP协议文档--The Constrained Application Protocol (CoAP)] [CoAP协议学习--CoAP基础] [CoAP学习

用ajax获取后台数据,返回json数据,怎么在前台使用?

用ajax获取后台数据,返回json数据,怎么在前台使用呢? 后台 C# code ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 if (dataType == "SearchCustomer")                 {                     int ID;                     if (Int32.TryParse(CustomerID, out ID))                     {