国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

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

2019-11-20 21:07:54
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

jquery提供的簡(jiǎn)化版的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", "");
                    });
}


在開(kāi)發(fā)的時(shí)候,要接受json格式的返回值時(shí),上面的方法貌似不能行,上面的方法貌似接受的是text的文本行。因此,采用jQuery的底層Ajax實(shí)現(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) {//請(qǐng)求錯(cuò)誤 時(shí)執(zhí)行的方法
                    alert("error!" + errorThrown);
                    $("#divWait").hide();
                    $("#btnPost").attr("disabled", "");
                },
                success: function (data, txtSataus) {//請(qǐng)求成功時(shí)執(zhí)行的方法
                    showContent(data.content, data.createdate);
                    $("#divWait").hide();
                    $("#btnPost").attr("disabled", "");
                }

                });
        }


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

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

如果是返回的html或者text的話(huà)可以如下寫(xiě)法

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

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

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

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

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

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

舉個(gè)例子:

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

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

這樣的代碼可以直接寫(xiě)出來(lái),在vs2010的代碼編輯器中還可以有代碼提示。很強(qiáng)大。

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 的摘要說(shuō)明
    /// </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();         
                //這里使用自定義日期格式,如果不使用的話(huà),默認(rèn)是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;
            }
        }
    }
}

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 巨鹿县| 琼中| 兴隆县| 曲麻莱县| 曲阳县| 平顶山市| 万宁市| 宁河县| 浠水县| 隆回县| 宜兰县| 乃东县| 舞钢市| 通河县| 阜新| 济南市| 隆林| 万载县| 育儿| 嵊泗县| 临沂市| 清原| 乌什县| 涟水县| 辉南县| 包头市| 丹东市| 贵溪市| 临沭县| 金湖县| 河津市| 西贡区| 铜川市| 嘉祥县| 萝北县| 田阳县| 芦山县| 奉节县| 台江县| 榕江县| 沈丘县|