Jquery .ajax方法分析(二)
来源:互联网 发布:淘宝客佣金代扣时间 编辑:程序博客网 时间:2024/06/03 03:51
http://www.cnblogs.com/jams742003/archive/2009/12/30/1636185.html
访问ws,而web方法再是字符串返回类型。这次通过response来响应请求。所处环境:.net3.5,而webservice 不再添加修饰标签:[System.Web.Script.Services.ScriptService]
(一)Hello
·ws
[WebMethod]
public void HelloWorld()
{
HttpResponse Response =HttpContext.Current.Response;
Response.ContentEncoding = System.Text.Encoding.Default;
Response.ContentType = "application/json";
Response.Write("Hello World!");
}
·ajax post
function ajax_wsHell() {
$.ajax({
type: "post",
contentType: "application/json",
url: "ajax_2.asmx/HelloWorld",
success: function(data) {
alert(data);
}
});
}
Hello World!
·对于纯字串来说(不是json字串),响应的数据是真的字串。
(二)Customer
·ws
[WebMethod]
public void GetCustomer()
{
Customer customer = new Customer
{ Unid = 1, CustomerName = "宋江", Memo ="天魁星", Other ="黑三郎" };
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);
HttpResponse Response =HttpContext.Current.Response;
Response.ContentEncoding = System.Text.Encoding.Default;
Response.ContentType = "application/json";
Response.Write(strJson);
}
·ajax post
function ajax_wsCustomer() {
$.ajax({
type: "post",
contentType: "application/json",
url: "ajax_2.asmx/GetCustomer",
success: function(data) {
var jsonObject = $.jsonToObject(data);
var tt = '';
$.each(jsonObject, function(k, v) {
tt += k + ":" + v +"<br/>";
});
$("#divmessage").html(tt);
}
});
}
{"Unid":1,"CustomerName":"宋江","Memo":"天魁星","Other":"黑三郎"}
对于json字串来说,它返回的是一个纯的json字串,不再是以d为key的k/v对。而对于json字串来说,转换到json对象很容易(. jsonToObject()方法,我以前的随笔中有介绍)。
(三)customer list
·ws
[WebMethod]
public void GetCustomerList()
{
Customer customer = new Customer
{ Unid = 1, CustomerName = "宋江", Memo ="天魁星", Other ="黑三郎" };
Customer customer2 = new Customer
{ Unid = 2, CustomerName = "吴用", Memo ="天机星", Other ="智多星" };
List<Customer> _list =new List<Customer>();
_list.Add(customer);
_list.Add(customer2);
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(_list);
HttpResponse Response =HttpContext.Current.Response;
Response.ContentEncoding = System.Text.Encoding.Default;
Response.ContentType = "application/json";
Response.Write(strJson);
}
·ajax post
function ajax_wsCustomerList() {
$.ajax({
type: "post",
contentType: "application/json",
url: "ajax_2.asmx/GetCustomerList",
success: function(data) {
var jsonObject = $.jsonToObject(data);
var tt = '';
$.each(jsonObject, function(k, v) {
$.each(v, function(kk, vv) {
tt += kk + ":" + vv +"<br/>";
});
});
$("#divmessage").html(tt);
}
});
}
[
{"Unid":1,"CustomerName":"宋江","Memo":"天魁星","Other":"黑三郎"},
{"Unid":2,"CustomerName":"吴用","Memo":"天机星","Other":"智多星"}
]
可以看出,得到的也是纯json字串。
(四)带参数
·ws
[WebMethod]
public void GetCustomerListWithPara(int iUnid)
{
Customer customer = new Customer
{ Unid = 1, CustomerName = "宋江", Memo ="天魁星", Other ="黑三郎" };
Customer customer2 = new Customer
{ Unid = 2, CustomerName = "吴用", Memo ="天机星", Other ="智多星" };
List<Customer> _list =new List<Customer>();
_list.Add(customer);
_list.Add(customer2);
var cus = from q in _list
where q.Unid == iUnid
select q;
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(cus);
HttpResponse Response =HttpContext.Current.Response;
Response.ContentEncoding = System.Text.Encoding.Default;
Response.ContentType = "application/json";
Response.Write(strJson);
}
·ajax post
function ajax_wsCustomerListWithPara() {
$.ajax({
type: "post",
url: "ajax_2.asmx/GetCustomerListWithPara",
data: ({ iUnid: 1 }),
dataType: "json",
success: function(data) {
var tt = '';
$.each(data, function(k, v) {
$.each(v, function(kk, vv) {
tt += kk + ":" + vv +"<br/>";
});
});
$("#divmessage").html(tt);
}
});
}
[{"Unid":1,"CustomerName":"宋江","Memo":"天魁星","Other":"黑三郎"}]
这里又返回了一个json对象。而不是一个json串。而且,没有给客户端ajax请求添加 contentType参数。
综述:
·对于请求由Response响应的json字串值,有很大的便利性,但对于带参数的请求,返回的是一个json对象,这可以直接处理。
·而web服务类不必添加修饰标签,这在.net2.0中应该也是可以的,是普遍的。所以,在通过web服务实现ajax时,可以采用这一通用方法。
·.ajax()方法的参数要合适,否则会不出结果,其根源在于jquery类库中对function :ajax的定义
·ajax方法是.getJSON(),.get(),.post()的根。而这三种方法是做为.ajax方法的一种特殊情况集来表达的。
- Jquery .ajax方法分析(二)
- Jquery .ajax方法分析(二)
- Jquery .ajax方法分析
- Jquery .ajax方法分析
- Jquery .ajax方法分析(一)
- Jquery .ajax方法分析(一)
- JQuery的AJAX调用(二)——AJAX方法
- jQuery - AJAX get() 和 post() 方法(二十六)
- jQuery源码分析之$.ajax方法
- dhtmlx5源码解析(二)全局方法分析date&&ajax
- AJAX(二)jquery ajax
- jQuery ajax - ajax() 方法(十七)
- jQuery ajax - ajax() 方法
- jQuery ajax - ajax() 方法
- jQuery ajax - ajax() 方法
- jQuery ajax - ajax() 方法
- jQuery ajax - ajax() 方法
- jQuery ajax - ajax() 方法
- Android Studio 1.0.1 + NDK-r10 编译本地代码问题解决办法
- 验证码识别
- PhoneGap详解
- sql语句--记忆
- Android开发之蓝牙
- Jquery .ajax方法分析(二)
- android SDK manager 无法获取更新版本列表的解决方案——国内教育资源镜像
- absolute与float布局会使得高度宽度 塌陷,仅仅保留边框。relative不会塌陷
- uva 112 Tree Summing
- 祝钒刚回应王婧涉毒:刚开始交往 未使用她的钱
- ORACLE—007:查找删除的数据
- 我在好程序员二期Android班,历练进行中
- GB312-URL编码
- 1412261712-蓝桥杯-基础练习 闰年判断