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...
870 0
|
Web App开发 XML JSON
jquery的.get方法说解
准备工作 ·Customer类 public class Customer {     public int Unid { get; set; }     public string CustomerName { get; set; }     public string Memo { ...
625 0
|
6月前
|
JavaScript 前端开发
百叶窗效果的jQuery幻灯片插件
百叶窗效果的jQuery幻灯片插件
|
6月前
|
JavaScript
jquery无限循环内容滑块插件
jquery无限循环内容滑块插件
|
6月前
|
JavaScript
简单轻量级的jquery图表插件
简单轻量级的jquery图表插件
|
6月前
|
JavaScript
jQuery响应式内容选项卡插件
jQuery响应式内容选项卡插件
|
6月前
|
JavaScript 前端开发
带完成百分比的jQuery表单插件
带完成百分比的jQuery表单插件
|
6月前
|
JavaScript 内存技术
支持多种动画特效的响应式jQuery幻灯片插件
支持多种动画特效的响应式jQuery幻灯片插件
|
8月前
jQuery+Slick插件实现游戏人物轮播展示切换源码
jQuery+Slick插件实现游戏人物轮播展示切换源码
111 14
|
9月前
|
JavaScript 前端开发
jQuery和CSS3滑动展开菜单按钮插件
这是一款jQuery和CSS3滑动展开菜单按钮插件。该滑动展开菜单按钮在用户点击主菜单按钮之后,子菜单以滑动的方式依次展开
129 21