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/

转载请注明:博客园

目录
相关文章
|
6月前
|
JavaScript 前端开发
jquery中有.post,.get,$.getJSON为什么没postJSON
jquery中有.post,.get,$.getJSON为什么没postJSON
|
9月前
|
JSON 前端开发 JavaScript
AJAX(GET POST请求、 jQuery axios 发送请求、跨域--cors、请求超时、网络异常、放弃请求、重复发送请求)(三)
AJAX(GET POST请求、 jQuery axios 发送请求、跨域--cors、请求超时、网络异常、放弃请求、重复发送请求)(三)
|
9月前
|
缓存 JSON 前端开发
AJAX(GET POST请求、 jQuery axios 发送请求、跨域--cors、请求超时、网络异常、放弃请求、重复发送请求)(二)
AJAX(GET POST请求、 jQuery axios 发送请求、跨域--cors、请求超时、网络异常、放弃请求、重复发送请求)(二)
|
9月前
|
XML 数据采集 Web App开发
AJAX(GET POST请求、 jQuery axios 发送请求、跨域--cors、请求超时、网络异常、放弃请求、重复发送请求)(一)
AJAX(GET POST请求、 jQuery axios 发送请求、跨域--cors、请求超时、网络异常、放弃请求、重复发送请求)
|
12月前
|
缓存 JavaScript 前端开发
开心档之jQuery - AJAX get() 和 post() 方法
【摘要】 jQuery - AJAX get() 和 post() 方法jQuery get() 和 post() 方法用于通过 HTTP GET 或 POST 请求从服务器请求数据。HTTP 请求:GET vs. POST两种在客户端和服务器端进行请求-响应的常用方法是:GET 和 POST。GET - 从指定的资源请求数据POST - 向指定的资源提交要处理的数据GET 基本上用于从服务器获得(取...
|
JavaScript 前端开发 PHP
jquery $.post 序列化表单ajax提交
jquery $.post 序列化表单ajax提交
96 0
|
JavaScript 前端开发 PHP
jQuery|AJAX get() 和 post()
jQuery get() 和 post() 方法用于通过 HTTP GET 或 POST 请求从服务器请求数据。 HTTP 请求:GET vs. POST 两种在客户端和服务器端进行请求-响应的常用方法是:GET 和 POST。
1152 0
|
JavaScript 前端开发 数据格式
jquery 中$.post获取MVC Controller中JsonResult返回包含LIst<Model>类型的子List<Model>的高级使用方法
比如JsonResult中返回return Json(models);的models结构如下: models返回含有四个集合的序列,每个集合的序列中又包含一个子集合序列“Child”。 问题是如果我们使用Jquery的$.
1232 0
|
Web App开发 XML JavaScript
jQuery $.get $.post $.getJSON 详解【转发】
When Ajax meets jQuery 基于AJAX的应用现在越来越多,而对于前台开发人员来说,直接和底层的HTTPRequest打交道又不是一件令人愉快的事情。jQuery既然封装了 JavaScript,肯定已经考虑过AJAX应用的问题。
1338 0