经过一个多星期的研究,各种查找资料终于自己实现了Jquery  Ajax Json ashx 的前后台数据交流功能

首先一点,Ajax只能对应一个ashx文件,多余两个,如果打开异步传输的async: true,第二个无法返回数据。

第二post和get的方式除了w3shcool中说的HTTP 方法:GET 对比 POST,在后台的代码上也有一定的区分

使用Jquery封装的Ajax比较简单,Json需要解析。

1.新建类Jsonconvert.cs,用于Json的解析和转换

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Runtime.Serialization;usingSystem.Runtime.Serialization.Json;usingSystem.ServiceModel.Web;using System.IO;//MemoryStream

using System.Text;//StringBuilder

///

///Json 的摘要说明///

public static classJsonconn

{public static string ToJsJson(this objectitem) {

DataContractJsonSerializer serializer= newDataContractJsonSerializer(item.GetType());using (MemoryStream ms = newMemoryStream()) {

serializer.WriteObject(ms, item);

StringBuilder sb= newStringBuilder();

sb.Append(Encoding.UTF8.GetString(ms.ToArray()));returnsb.ToString();

}

}///

///Json反序列化,用于接收客户端Json后生成对应的对象///

public static T FromJsonTo(this stringjsonString) {

DataContractJsonSerializer ser= new DataContractJsonSerializer(typeof(T));

MemoryStream ms= newMemoryStream(Encoding.UTF8.GetBytes(jsonString));

T jsonObject=(T)ser.ReadObject(ms);

ms.Close();returnjsonObject;

}

}

View Code

2.新建类用于保存属性

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;///

///data 的摘要说明///

public classdata

{publicdata()

{//

//TODO: 在此处添加构造函数逻辑//}public string d { get; set; }

}

3.前台Ajax的写法

$.ajax({

url:'Ashx/Registerlastchk.ashx',

type:'post',//contentType: "application/json; charset=utf-8",

data: {username:$("#Username").val(),password: $("#Password").val(),email:$("#signup_email").val(),pass:1} ,

datatype:"json",

async:true,

beforeSend:function() {

$("#div_signing").show();

$('#SignUpButton').attr('disabled', "true"); //添加disabled属性

},

error:function(data){

$("#div_signing_info").html("连接服务器失败");

},

success:function(data) {var datastring = JSON.parse(data);//******很多代码都丢了这段

alter(datastring.d) ;

}

View Code

4.ashx中的写法:

usingSystem;usingSystem.Web;usingSystem.Collections.Generic;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Runtime.Serialization;usingSystem.Runtime.Serialization.Json;usingSystem.ServiceModel.Web;using System.IO;//MemoryStream

using System.Text;//StringBuilder

///

///建立新用户///

public classRegisterlastchk : IHttpHandler {///

///先检查名称,建立新用户///

///

public voidProcessRequest (HttpContext context) {string pass = GetJsonClient("pass", context);if (pass == "1") {string password = GetJsonClient("password", context);string email = GetJsonClient("email", context);string name = GetJsonClient("username", context);

data reajax=newdata();var manager = newRegisterck();if (string.IsNullOrEmpty(name)) {throw new Exception("Username is empty");

}if(manager.Checkname(name)) {

NewUser signnew= newNewUser();var result =signnew.signnewuser(name, password, email);if(result) {

reajax.d= "1";//注册完成

context.Response.Write(reajax.ToJsJson());

}else{

reajax.d= "0"; //注册失败

context.Response.Write(reajax.ToJsJson());

}

}else{

reajax.d= "-1"; //用户名存在

context.Response.Write(reajax.ToJsJson());

}

}

}public string GetJsonClient(stringname, HttpContext context) {

context.Response.ContentType= "text/plain";

context.Response.Charset= "utf-8";if (context.Request[name] == null) { return null; }string temp =context.Request[name];returntemp;

}///

///Json序列化,用于发送到客户端///

public boolIsReusable {get{return true;

}

}

}

View Code

post的时候原来是datatype是Json,ContentType 最好为"text/plain",试过“Aplication/Json”无获得数据,但是get方式时候可以货到,Post时候为null,原因不明,应该使用方法中 T FromJsonTo,也是极好的,还未做实验,今天太晚了,明天试试

context.Response.ContentType = "text/plain";

context.Response.Charset= "utf-8";

ashx 后台 解析json_Jquery Ajax Json ashx 实现前后台数据传输相关推荐

  1. ajax ashx 请选择文件,ajax+jquery+ashx如何实现上传文件

    ajax+jquery+ashx如何实现上传文件 第一:建立Default.aspx页面 ajax图片上传 function upload(){ var path = document.getElem ...

  2. ajax ashx调试,asp.net——Ajax与ashx异步请求的简单案例

    Ajax与ashx异步请求的简单案例: 前台页面(aspx): function gettext() { var intxt = $("#intxt").val(); $.ajax ...

  3. ajax+php+jq+面向对象,php+jquery+ajax+json的一个最简单实例

    html页面: $(function(){ $("#send").click(function(){ var cont = $("input").seriali ...

  4. ajax调用ashx的方法,jquery.ajax请求aspx和ashx的异同 Jquery Ajax调用aspx页面方法

    1.jquery.ajax请求aspx 请求aspx的静态方法要注意一下问题: (1)aspx的后台方法必须静态,而且添加webmethod特性 (2)在ajax方法中contentType必须是&q ...

  5. ajax调用ashx页面内的方法

    //$.ajax的post方式 function CommentAll() {$.ajax({url: "/ashx/myzhuye/Detail.ashx",type: &quo ...

  6. 前台传json ajax,ajax使用json数组------前端往后台发送json数组及后台往前端发送json数组...

    1.引子 2.前端往后台传输json数据 JSON.stringify()方法将json对象转为字符串形式.如果不使用**JSON.stringify()**方法,后台的java代码将不能获得json ...

  7. jquery ajax json 数据的遍历

    需求:进行ajax请求后,后台传递回来以下json数据 { "data":[ {"id":"1","name":&quo ...

  8. web前端之后的4天(Ajax,json,redis,maven)

    web前端之后的10天(Ajax,json,redis,maven) day2(还是前端的内容) 1.AJAX 2.JSON AJAX: 1.概念:ASynchronous JavaScrip And ...

  9. AJAX+JSON【练习】实现简易的点赞功能

    AJAX+JSON[练习]实现简易的点赞功能 1.前言 2.AJAX实现前后端数据传递 3.JSON与Java对象之间的转换 4.数据库相关配置 5.jQuery的JS文件,lib中的Jar包 6.代 ...

最新文章

  1. Java常用多线程辅助工具---countdownLatch
  2. C++和C#相互调用COM组件的方法简介
  3. 算法导论——DFS深度优先搜索
  4. 回顾build 2016:你好,这是微软迄今最好的Windows开发平台
  5. selenium中Chrome和Firefox浏览器驱动的使用和版本对应
  6. 还原python源码_python 的混淆后的代码可以还原么
  7. learning-oracle-how2j
  8. Excel 2010 VBA 入门 002 录制和运行宏
  9. ArcGIS——计算几何——面积/周长禁用
  10. 【msm8953】gpio口模拟pwm详细步骤
  11. Python 快速搭建文件上传服务器
  12. 《Adobe Photoshop CS5中文版经典教程(全彩版)》目录—导读
  13. Tomcat中 appBase和docBase配置及默认管理页面
  14. docker onlyoffice7.1.1 word excel ppt在线编辑、在线预览_添加中文字体和中文字号_02
  15. 附录2-PS基本操作
  16. 程序员写好技术文章的几点小技巧,简述java内存模型面试
  17. 网上购书系统的设计与实现(PHP+MySQL)
  18. 疫情期间,大型企业如何保障日常会议的正常进行?
  19. 到底Python编程好学吗 为什么会如此受欢迎
  20. 学习笔记之编程达到一个高的境界就是自制脚本语言(图)

热门文章

  1. 很遗憾未能成功连接服务器神武,神武暗恋这件小事:很遗憾没有成为你的英雄_ 叶子猪神武...
  2. 自旋电子学材料与器件学习结课记录(论文)
  3. 阿里巴巴Java后端社招5面技术总结(Offer已拿)
  4. 2023最新SSM计算机毕业设计选题大全(附源码+LW)之java杨柳农贸市场摊位管理系统qr3ri
  5. 最强蜗牛击败毁灭机器人_最强蜗牛恶魔形态所有阶级详解_恶魔形态介绍
  6. 分布式系统架构经典资料--左耳
  7. asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).
  8. 如何注册新加坡lol服务器,英雄联盟手游新加坡服安卓账号怎么注册
  9. 计算机软件系统崩溃,为什么我的电脑WIN7系统老是这个程序会崩溃
  10. 神注释大全(欢迎补充)