• 技术文章 >web前端 >js教程

    Json实现异步请求提交评论无需跳转其他页面_jquery

    2016-05-16 16:34:16原创481
    主要将代码粘贴,通过阅读代码理解其中的相关逻辑。

    html代码:

    评论:

    姓名:

    内容:

    已有评论:

    js代码:

    $("#send").click(function () { 
    $.get("doSave.ashx", {   //调用json插件 
    u_name: $("#username1").val(), //json数据/值对化 
    u_cont: $("#content").val() 
    }, function (data) 
    var uName = data.username; //注:此处的username与doSave.ashx中的dic.add("username",uname)中的username相对应的 
    var uCont = data.content; 
    var txtHtml = "
    " + uName + ":

    " + uCont + "

    " $("#resText").html(txtHtml); //将返回的数据添加到页面上 }, "json"); })

    插件代码:

    <%@ WebHandler Language="C#" Class="doSave" %> 
    
    using System; 
    using System.Web; 
    
    public class doSave : IHttpHandler 
    { 
    
    public void ProcessRequest(HttpContext context) 
    { 
    
    var dic = new System.Collections.Generic.Dictionary(); //存储的集合 
    string jsonStr = "{}"; //新建字符串jsonStr 
    
    context.Response.ContentType = "text/json"; //定义返回的内容类型为json 
    
    string uname = context.Request.QueryString[0]; //获取请求参数中第一个参数,也可以直接使用uname 
    
    string commet = context.Request.QueryString[1]; //定义字符串uname、commet为context请求查询的字符串context.Request.Params["username"];QyertStrubg:查询字符串 
    
    dic.Add("username", uname); //将字符串添加到对象中 
    
    dic.Add("content", commet); 
    
    jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(dic); //序列化集合为json字符串 
    
    context.Response.Write(jsonStr); 
    } 
    
    public bool IsReusable 
    { 
    get 
    { 
    return false; 
    } 
    } 
    
    }

    此处效果即为,在输入框中输入相关文字,点击提交,下方会自动将书写的文字进行展示,无需跳转其他页面。

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:Json 异步请求
    上一篇:JavaScript中获取鼠标位置相关属性总结_javascript技巧 下一篇:js网页右下角提示框实例_javascript技巧
    Web大前端开发直播班

    相关文章推荐

    • Angular项目中怎么使用 SASS 样式• Vercel是什么?怎么部署Node服务?• 一文聊聊Angular中的生命周期• Angular项目如何上线?结合nginx来聊聊上线流程!• 什么是流(Stream)?如何理解Nodejs中的流
    1/1

    PHP中文网