Jquery的.post说解(二)

简介: $.post 调用webservice,通过Response来返回数据。 (一)Hello ·ws [WebMethod]public void HelloWorld(){    HttpResponse Response = HttpContext.

$.post

调用webservice,通过Response来返回数据。

(一)Hello

·ws

[WebMethod]
public   void  HelloWorld()
{
    HttpResponse Response 
=  HttpContext.Current.Response;
    Response.ContentEncoding 
=  System.Text.Encoding.Default;
    Response.Write(
" Hello world! " );
}

 

·ajax post

function  ajaxVoidHello() {
    $.post(
    
" post_2.asmx/HelloWorld " ,
    
function (data) {
        
var  jsonString  =  data.text;
        $(
" #divmessage " ).html(data);
    },
    
" json "
    );
}

 

客户端得到的数据类型为string类型。

(二)得到客户实体

·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);
}

 

这里ContentType很重要,这里要保留,即使空值也可以。

·ajax post

function  ajaxGetCustomer() {
    $.post(
    
" post_2.asmx/GetCustomer " ,
    
function (data) {
        
var  jsonString  =  data;
        
var  jsonObject  =  $.jsonToObject(jsonString); 

        
var  tt  =   '' ;
        $.each(jsonObject, 
function (k, v) {
            tt 
+=  k  +   " : "   +  v  +   " <br/> " ;
        });

        $(
" #divmessage " ).html(tt);
    },
   
" json "
);}

 

请求json数据,返回的是一个字符串,就是json字串,然后处理。

(三)得到客户集

·ws

[WebMethod]
public   void  GetCustomersList()
{
    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  ajaxGetCustomerList() {
    $.post(
    
" post_2.asmx/GetCustomersList " ,
    
function (data) {
        alert(data);
        
var  jsonString  =  data;
        
var  jsonObject  =  $.jsonToObject(jsonString); 

        
var  tt  =   '' ;
        $.each(jsonObject, 
function (k, v) {
            $.each(v, 
function (kk, vv) {
                tt 
+=  kk  +   " : "   +  vv  +   " <br/> " ;
            });
        });
        $(
" #divmessage " ).html(tt);
    },
   
" json "
);}

 

这些很容易理解了,返回的是json字串。处理字串就可以了。

(四)带参数的

·ws

[WebMethod]
public   void  GetCustomersListWithPara( 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 q 
=  _list.Where(p  =>  p.Unid  ==  iUnid); 

    
string  strJson  =  Newtonsoft.Json.JsonConvert.SerializeObject(q); 

    HttpResponse Response 
=  HttpContext.Current.Response;
    Response.ContentEncoding 
=  System.Text.Encoding.Default;
    Response.ContentType 
=   "" ;
    Response.Write(strJson);
}

 

·ajax post

function  ajaxGetCustomerListWithPara() {
    $.post(
    
" post_2.asmx/GetCustomersListWithPara " ,
    { iUnid: 
1  },
    
function (data) {
        
var  tt  =   '' ;
        $.each(data, 
function (k, v) {
            $.each(v, 
function (kk, vv) {
                tt 
+=  kk  +   " : "   +  vv  +   " <br/> " ;
            })
        });
        $(
" #divmessage " ).html(tt);
    },
   
" json "
);}

 

这里返回的是[object,object]类型。

服务端的ContentType可以设置为空。

 

 

博客园大道至简

http://www.cnblogs.com/jams742003/

转载请注明:博客园

目录
相关文章
|
Web App开发 JSON 前端开发
Jquery的.post说解(一)
准备工作 ·Customer类 public class Customer{    public int Unid { get; set; }    public string CustomerName { get; set; }    public string Memo { get...
836 0
|
Web App开发 XML JSON
jquery的.get方法说解
准备工作 ·Customer类 public class Customer {     public int Unid { get; set; }     public string CustomerName { get; set; }     public string Memo { ...
578 0
|
11月前
|
JavaScript
Jquery插件知识之Jquery.cookie实现页面传值
Jquery插件知识之Jquery.cookie实现页面传值
54 0
|
12月前
|
JavaScript
jQuery 自定义插件
jQuery 自定义插件
44 0
|
12月前
|
JavaScript
jQuery 插件自用列表
jQuery 插件自用列表
50 0
|
4月前
|
JavaScript
jQuery图片延迟加载插件jQuery.lazyload
jQuery图片延迟加载插件jQuery.lazyload
|
3月前
|
设计模式 JavaScript 前端开发
必知的技术知识:jQuery插件开发精品教程,让你的jQuery提升一个台阶
必知的技术知识:jQuery插件开发精品教程,让你的jQuery提升一个台阶
41 1
|
28天前
|
JavaScript 前端开发 数据安全/隐私保护
Validform jQuery插件详解
【8月更文挑战第21天】
|
3月前
|
JavaScript Perl PHP
一篇文章讲明白jQuery插件之jqueryeditableplugin
一篇文章讲明白jQuery插件之jqueryeditableplugin
22 0
|
12月前
|
JavaScript
jQuery 自定义插件(详细步骤)
jQuery 自定义插件(详细步骤)
117 0