欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery ajax,ashx,json的用法總結(jié)

 更新時間:2014年02月12日 09:11:51   作者:  
本篇文章主要是對jquery ajax,ashx,json的用法進行了詳細的總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助

jquery提供的簡化版的ajax調(diào)用方法通常如下:

復(fù)制代碼 代碼如下:

    function post() {
    $("#divWait").show();
    $("#btnPost").attr("disabled", "disabled");
    $.post("../PostIt.ashx",
                    {
                        msgContent: $("#msgContent").val()
                    },
                    function (data) {
                        if (data.indexOf('OK') > -1) {
                            alert(data);
                        }
                        else {

                            }
                        $("#divWait").hide();
                        $("#btnPost").attr("disabled", "");
                    });
}


在開發(fā)的時候,要接受json格式的返回值時,上面的方法貌似不能行,上面的方法貌似接受的是text的文本行。因此,采用jQuery的底層Ajax實現(xiàn)方法。

該方法參數(shù)也很多,具體可看幫助文檔。本人的常規(guī)用法

復(fù)制代碼 代碼如下:

    function doPostAjax(){
            $("#divWait").show();
            $("#btnPost").attr("disabled", "disabled");
            $.ajax({
                url: '../PostIt.ashx',
                type: 'POST',
                dataType: 'json',
                data: { msgContent: $("#msgContent").val() },
                timeout: 60000,
                error: function (XMLHttpRequest, textStatus, errorThrown) {//請求錯誤 時執(zhí)行的方法
                    alert("error!" + errorThrown);
                    $("#divWait").hide();
                    $("#btnPost").attr("disabled", "");
                },
                success: function (data, txtSataus) {//請求成功時執(zhí)行的方法
                    showContent(data.content, data.createdate);
                    $("#divWait").hide();
                    $("#btnPost").attr("disabled", "");
                }

                });
        }


在ashx代碼段,要設(shè)置好返回的格式。

context.Response.ContentType = "application/json";

如果是返回的html或者text的話可以如下寫法

context.Response.ContentType = "text/plain";

如果ajax方法中設(shè)置的返回值是json時,ashx代碼返回的格式必須是json格式的數(shù)據(jù)。
把一個對象轉(zhuǎn)換成json格式,常用方法就是采用開源的第三方類庫json.net,Newtonsoft.Json.dll.

JsonConvert.SerializeObject方法就可以轉(zhuǎn)換了。返回json格式后,jquery就可以采用XXX.xxx的方式獲取值了。

JsonConvert在處理datetime格式的時候,會返回類似1198908717056的絕對值,因此,在處理datetime的時候,要做一下轉(zhuǎn)換。具體語句如下:

IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();          
//這里使用自定義日期格式,如果不使用的話,默認是ISO8601格式           
timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
string output = JsonConvert.SerializeObject(m, Newtonsoft.Json.Formatting.Indented, timeConverter);

此處順便提一下,javascript對json格式的數(shù)據(jù)有著天生的處理能力,非常好的兼容json格式數(shù)據(jù)。

舉個例子:

復(fù)制代碼 代碼如下:

    function pppp() {
           var person = { "name": "jack", "age": 24,"sex": true };
           alert(person.name);
           alert(person.age);
           alert(person.sex);
           }

這樣的代碼可以直接寫出來,在vs2010的代碼編輯器中還可以有代碼提示。很強大。

ashx完整代碼如下:

復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

    namespace nnn
{
    /// <summary>
    /// PostIt 的摘要說明
    /// </summary>
    public class PostIt : IHttpHandler
    {

            public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            try
            {
                string msgContent = context.Request["msgContent"] ?? "";
                ModelContent m = new ModelContent()
                {
                    author = "",
                    categoryid = -1,
                    title = "",
                    content = msgContent,
                    datetime = DateTime.Now,
                    key = "",
                    createdate = DateTime.Now,
                    lastmodifydate = DateTime.Now,
                    ip = context.Request.UserHostAddress

                    };

                    //BLLContent bll = new BLLContent();
                //bll.Add(m);

                    IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();         
                //這里使用自定義日期格式,如果不使用的話,默認是ISO8601格式          
                timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
                string output = JsonConvert.SerializeObject(m, Newtonsoft.Json.Formatting.Indented, timeConverter);
                context.Response.Write(output);
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message);
            }

            }

            public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

相關(guān)文章

最新評論