jquery发送ajax请求返回数据格式
jquery向服务器发送一个ajax请求后,可以返回多种类型的数据格式,包括:html,xml,json,text等。
1.html格式的数据
"<div class='comment'><h6> "+username+" :</h6><p class='para'> "+content+" </p></div>"
服务器端返回数据格式是html片段,因此不需要经过处理就可以将新的html数据插入到主页面中,这种方法虽然简便,但是重用性不强。
$.ajax({
type:"POST",
url:"Handler.ashx",
dataType:html,
data:{username:$("#name").val(),password:$("#pwd").val()},
success:function(data){
$("#result").html(data);
}
}
2.XML格式的数据
Response.Write("<?xml version=""1.0"" encoding=""utf-8""?>")
Response.Write("<comments>")
Response.Write("<comment username='"+username+"'>")
Response.Write("<content>"+content+"</content>")
Response.Write("</comment>")
Response.Write("</comments>")
Response.End();
由于服务器端返回的数据格式是XML文档,因此需要对文档的数据进行处理。
$.ajax({
type:"POST",
url:"Handler.ashx",
dataType:xml,
data:{username:$("#name").val(),password:$("#pwd").val()},
success:function(data){
var username = $(data).find("comment").attr("username");
var content = $(data).find("comment content").text();
var txtHtml = "<div class='comment'><h6>"+ username+":</h6><p class='para'>"+content+"</p></div>";
$("#result").html(txtHtml)
}
}
3.JSON格式的数据
Response.Write("{ username : '"+username+"' , content : '"+content+"'}")
由于服务器端返回的数据格式是JSON文档,因此也需要对文档的数据进行处理,但是JSON文档比较XML文档更容易解析。
$.ajax({
type:"POST",
url:"Handler.ashx",
dataType:json,
data:{username:$("#name").val(),password:$("#pwd").val()},
success:function(data){
var username = data.username;
var content = data.content;
var txtHtml = "<div class='comment'><h6>"+ username+":</h6><p class='para'>"+content+"</p></div>";
$("#result").html(txtHtml)
}
}
jquery发送ajax请求返回数据格式的更多相关文章
- Jquery发送ajax请求以及datatype参数为text/JSON方式
Jquery发送ajax请求以及datatype参数为text/JSON方式 1.方式一:datatype:'text' 2.方式二:datatype:'JSON' 3.使用gson-1.5.jar包 ...
- 学习AJAX必知必会(4)~JQuery发送Ajax请求
一.JQuery发送Ajax请求 ■ 对于get和post请求,jQuery内部封装了Ajax请求的4个步骤和数据格式的设置 ■ 对于Ajax通用请求,jQuery内部封装了Ajax请求的4个步骤和数 ...
- jQuery发送ajax请求
利用jquery发送ajax请求的几个模板代码. $.ajax({ async : false, type: 'POST', dataType : "json", url: &qu ...
- JQuery发送ajax请求不能用数组作为参数
JQuery发送ajax请求不能用数组作为参数,否则会接收不到参数, 一.js代码如下: $('#delete-button').click(function(){ var select ...
- jQuery发送Ajax请求以及出现的问题
普通jQuery的Ajax请求代码如下: $.ajax({ type: 'POST', url: "http://xxx/yyy/zzz/sendVerifyCode", data ...
- IE9下JQuery发送ajax请求失效
最近在做项目的时候,测试PC端网页,在IE9下会失效,不能正常的发送POST请求,经过仔细的排查,发现是IE9下JQuery发送ajax存在跨域问题. 目前有两种解决方案: 解决方案一: 设置浏览 ...
- python测试开发django-50.jquery发送ajax请求(get)
前言 有时候,我们希望点击页面上的某个按钮后,不刷新整个页面,给后台发送一个请求过去,请求到数据后填充到html上,这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新.Ajax可以完美的 ...
- 关于解决JQuery发送Ajax请求后,IE缓存数据不更新的问题
http://www.cnblogs.com/lys_013/archive/2013/08/07/3243435.html 今天在做ajax页面无刷新请求后台服务器数据的时候,IE下遭遇Ajax缓存 ...
- JQuery发送Ajax请求出现 500 Internal Server Error
ajax返回,readyState=4,status=500,chrome f12提示,提示服务器内部错误 我采用 http://q.cnblogs.com/q/69745/的解决办法根本不行,也不是 ...
随机推荐
- [转]服务器自动化操作 RunDeck
From : http://www.oschina.net/p/rundeck/similar_projects?sort=view&lang=25 RunDeck 是用 Java/Grail ...
- C#字符格式化占位符
using System; using System.Diagnostics; using System.Text; using System.Collections; using System.Co ...
- PHP基础18:require和include
<?phph //1.通过 include 或 require 语句,可以将 PHP 文件的内容插入另一个 PHP 文件(在服务器执行它之前) //include 和 require 语句是相同 ...
- SpringMvc学习心得(五)控制器产生与构建
SpringMvc学习心得(五)控制器产生与构建 标签: springspring mvc框架 2016-03-22 15:29 140人阅读 评论(0) 收藏 举报 分类: Spring(4) ...
- 学习Shell脚本编程(目录)
所涉及的内容如下: Shell命令行的运行 编写.修改权限和执行Shell程序的步骤 在Shell程序中使用参数和变量 表达式比较.循环结构语句和条件结构语句 在Shell程序中使用函数和调用其他Sh ...
- [CareerCup] 8.6 Jigsaw Puzzle 拼图游戏
8.6 Implement a jigsaw puzzle. Design the data structures and explain an algorithm to solve the puzz ...
- java个人总结
20145230<Java程序设计>课程总结 每周读书笔记链接汇总 20145230<java程序设计>第0周学习总结:http://www.cnblogs.com/kobe2 ...
- WPF 得到子指定元素方法和得到指定子元素集合方法MvvM得到焦点
public class UIHelper { /// <summary> /// 在Visual里找到想要的元素 /// childName可为空,不为空就按名字找 /// </s ...
- grootJS ui控件定义
index13.html <html><head> <title>ui控件定义</title> <script src="jquery- ...
- Google浏览器导出书签
C:\users\用戶名\AppData\Local\Google\Chrome\User Data\Default\Bookmarks 這個文件就是書簽啊,復制一下就行了