Jquery的.post说解(一)

准备工作

·Customer类

 
public class Customer
{
    public int Unid { get; set; }
    public string CustomerName { get; set; }
    public string Memo { get; set; }
    public string Other { get; set; }
}  

jQuery.post( url, [data], [callback], [type] )

·url:加载页的地址

·data(optional):k/v对或序列化的字符串(.serialize()),参数

·callbakc(optional):数据成功加载后的执行函数

·type(optional):请求返回的数据格式,串型

(一)ashx文件

(1)请求单实体数据

·Ashx文件,这里不对返回的数据做显式的序列化。

Customer customer = new Customer 
  { Unid = 1, CustomerName = "宋江", Memo = "天魁星", Other = "黑三郎" }; context.Response.Write(customer);

·ajax post

 
function GetCustomer_Ashx() {
    $.post(
    "webdata/post_1.ashx",
    function(data) {
        var sx = $.JsonToObject(data);
        var tt = "";
        $.each(sx, function(k, v) {
            tt += k + ":" + v + "<br/>";
        })
        $("#divmessage").html(tt);
    },
    "json"
);}  

(2)请求实体集合

·ashx文件

 
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);     context.Response.Write(strJson);
 

·ajax post

 
function GetCustomer_AshxList() {
    $.post(
    "webdata/post_1.ashx",
    function(data) {
        var jsonObjects = $.jsonToObject(data);
        var tt = "";
        $.each(jsonObjects, function(k, v) {
            $.each(v, function(kk, vv) {
                tt += kk + ":" + vv + "<br/>";
            });
        });         $("#divmessage").html(tt);
    },
    "json"
    );
}
 

(3)带参数的请求

·ashx文件

在前者基础上添加了对请求参数的获取语句,并添加了linq查询

int iCustomerId = Convert.ToInt32(context.Request["iUnid"]);
        var cus = from q in _list
                  where q.Unid == iCustomerId
                  select q; string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(cus);

·ajax post

 
function GetCustomer_AshxWithPara() {
    $.post(
    "webdata/post_1.ashx",
    { iUnid: 1 },
    function(data) {
        var tt = "";
        $.each(data, function(k, v) {
            $.each(v, function(kk, vv) {
                tt += kk + ":" + vv + "<br/>";
            });
        });
        $("#divmessage").html(tt);
    },
    "json"
);}
 

注意,这里返回的直接是json对象[object,object],可以直接解析。

这种参数传递的方法是以k/v对格式传递,post还有一种方式,那就是.serialize()之后的字串。

(二)Web Service

(1)Hello

·ws

[WebMethod]
public string HelloWorld()
{
    return "Hello World";
}

·ajax post

 
function WebService_Hello() {
    $.post(
    "post_1.asmx/HelloWorld",
    function(data) {
        alert(data.text);
        $("#divmessage").html(data);
    },
    "json"
);}
 

这个web方法返回一个单独的字串。这是一个纯正的字串,对于客户端来说,这是一个object对象,但也可以理解为一个[object,object]对象,而它完整的数据格式可以理解为:{text: "Hello World"}

所以这里对它进行访问,可以如下:

·data.text 这种方式对应于Object.Property

·data["text"] 这种方式对应于Object["key"]

(2)json串

·ws

 
[WebMethod]
public string HelloWorld_Json()
{
    string strJson=
      @"{Unid:1,CustomerName:""宋江"",Memo:""天魁星"",Other:""黑三郎""}";
    return strJson;
}
 

·ajax post

 
function WebService_HelloJsonString() {
    $.post(
    "post_1.asmx/HelloWorld_Json",
    function(data) {
        var jsonString = data.text;
        var jsonObject = $.jsonToObject(jsonString);
        var tt = "";
        $.each(jsonObject, function(k, v) {
            tt += k + ":" + v + "<br/>";
        })         $("#divmessage").html(tt);
    },
    "json"
);}
 

虽然服务方法返回的是string类型的数据:

{Unid:1,CustomerName:"宋江",Memo:"天魁星",Other:"黑三郎"}

但客户端得到的数据却是object类型,可以理解为[object,object],也就是

{text:’{Unid:1,CustomerName:"宋江",Memo:"天魁星",Other:"黑三郎"}’}

客户端请求到的数据取到json字串,然后转换为json对象,后进行解析。

所以,在请求web服务方法时,如果方法返回字串类型,先要通过data.text得到做为唯一k/v对的v值,也就是json字串,然后再进行下一步操作。

(3)通过串行化返回json字串的web方法

·ws

 
[WebMethod]
public string GetCustomer_Json()
{
    Customer customer = new Customer
      { Unid = 1, CustomerName = "宋江", Memo = "天魁星", Other = "黑三郎" };     string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);
    return strJson;
}
 

·ajax post

 
function WebService_CustomerJsonString() {
    $.post(
    "post_1.asmx/GetCustomer_Json",
    function(data) {
        var jsonString = data.text;
        var jsonObject = $.jsonToObject(jsonString);
        var tt = "";
        $.each(jsonObject, function(k, v) {
            tt += k + ":" + v + "<br/>";
        })
        $("#divmessage").html(tt);
    },
    "json"
);}
 

这个方法与(2)相同道理。

(4)客户集

·ws

 
[WebMethod]
public string GetCustomerList_Json()
{
    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);
        return strJson;
}
 

·ajax post

 
function WebService_CustomerListJsonString() {
    $.post(
    "post_1.asmx/GetCustomerList_Json",
    function(data) {
        var jsonString = data.text;
        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字串,也就能正常解析出来。主要是理解返回的数据对象的格式。

(5)带参数的ws

·ws

 
[WebMethod]
public string GetCustomerList_JsonPara(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);
        return strJson;
}
 

·ajax post

 
function WebService_CustomerListJsonStringWithPara() {
    $.post("post_1.asmx/GetCustomerList_JsonPara",
    {iUnid:2},
    function(data) {
        var jsonString = data.text;
        var jsonObject = $.jsonToObject(jsonString);
        var tt = "";
        $.each(jsonObject, function(k, v) {
        $.each(v, function(kk, vv) {
        tt += kk + ":" + vv + "<br/>";
        });
        });
        $("#divmessage").html(tt);        
    }
 );}
 

带参数的post时,post函数的type部分不能以json格式请求返回。可以省略。

Jquery的.post说解的更多相关文章

  1. jquery的css详解(二)

    jq的工具方法style用于设置样式,jq的实例方法css在设置样式时就是调用的它,接下来分析一下源码. jQuery.extend({ ............................ st ...

  2. jQuery.attr() 函数详解

    一,jQuery.attr()  函数详解: http://www.365mini.com/page/jquery-attr.htm 二,jQuery函数attr()和prop()的区别: http: ...

  3. jQuery 事件用法详解

    jQuery 事件用法详解 目录 简介 实现原理 事件操作 绑定事件 解除事件 触发事件 事件委托 事件操作进阶 阻止默认事件 阻止事件传播 阻止事件向后执行 命名空间 自定义事件 事件队列 jque ...

  4. jQuery事件绑定、解绑、命名空间

    jQuery事件绑定.解绑.命名空间 <%@ page language="java" import="java.util.*" pageEncoding ...

  5. jQuery.ready() 函数详解

    jQuery.ready() 函数详解 ready()函数用于在当前文档结构载入完毕后立即执行指定的函数. 该函数的作用相当于window.onload事件. 你可以多次调用该函数,从而绑定多个函数, ...

  6. jquery inArray()函数详解

    jquery inarray()函数详解 jquery.inarray(value,array)确定第一个参数在数组中的位置(如果没有找到则返回 -1 ). determine the index o ...

  7. jQuery extend方法详解

    先说个概念的东西: jQuery为开发插件提拱了两个方法,分别是: $.fn.extend(item):为每一个实例添加一个实例方法item.($("#btn1") 会生成一个 j ...

  8. jQuery的观察者模式详解 转载

    jQuery的观察者模式详解 投稿:hebedich 本文主要是介绍了jQuery中on方法及trigger方法,以及围绕这个方法来体验的观察者模式,是篇非常不错的文章,对我们理解观察者模式很有帮助. ...

  9. 【转载】jQuery.extend 函数详解

    转载自:http://www.cnblogs.com/RascallySnake/archive/2010/05/07/1729563.html jQuery.extend 函数详解 JQuery的e ...

随机推荐

  1. 【Go】为什么用go; Golang Erlang 前世今生

    给自己一条退路,再次比较Erlang和Golang 2014-6-28 陈叶皓 chenyehao@gmail.com 雨天的周末,适合码字的时节... 一年前我开始学习go语言的时候,如获至宝,既有 ...

  2. C预处理,条件编译

    预处理是指在编译器之前运行,常以“#”开头 包含3个方面的内容: 1)宏定义与宏替换 2)文件包含 3)条件编译 宏定义与宏替换: 宏名一般大写,替换发生在编译之前,且是机械替换,不做语法检查,也不分 ...

  3. How to: Add SharePoint 2010 Search Web Parts to Web Part Gallery for Upgraded Site Collections

    When you upgrade to Microsoft SharePoint Server 2010, some of the new SharePoint Enterprise Search W ...

  4. MS SQL Server中的CONVERT日期格式化大全

    CONVERT 将某种数据类型的表达式显式转换为另一种数据类型.由于某些需求经常用到取日期格式的不同. 现以下可在SQL Server中将日期格式化. SQL Server 支持使用科威特算法的阿拉伯 ...

  5. SharePoint 优化显示WebParts

    在开发sharepoint中,经常遇到需要自定义显示列表中的一部分作为导航的内容, 如公告栏,新闻链接,最新动态等.... 我们通常需要显示一个列表的标题,并且限制字符长度, 外加一些条件,如按创建的 ...

  6. BZOJ 3875: [Ahoi2014]骑士游戏 dp+spfa

    题目链接: 题目 3875: [Ahoi2014]骑士游戏 Time Limit: 30 Sec Memory Limit: 256 MB 问题描述 [故事背景] 长期的宅男生活中,JYY又挖掘出了一 ...

  7. 【锋利的JQuery-学习笔记】遮罩层

    效果图:  鼠标移动到上面后---> html: <div id="jnBrandList"> <ul> <li> <a href= ...

  8. android 解析XML方式(一)

    在androd手机中处理xml数据时很常见的事情,通常在不同平台传输数据的时候,我们就可能使用xml,xml是与平台无关的特性,被广泛运用于数据通信中,那么在android中如何解析xml文件数据呢? ...

  9. 解谜谷歌 DevOps:什么特质可以打造世界级可靠系统?

    [编者按]本文是 Gene Kim 总结自对 Randy Shoup 两个小时的采访,主要关注谷歌 DevOps 的提升之道.本文系 OneAPM联合高效运维编译整理. Randy Shoup 曾协助 ...

  10. recursion lead to out of memory

    There are two storage areas involved: the stack and the heap. The stack is where the current state o ...