当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jquery ajax,ashx,json的用法总结

jquery ajax,ashx,json的用法总结

2019年03月21日  | 移动技术网IT编程  | 我要评论

代码如下:


    function post() {
    $("#pwait").show();
    $("#btnpost").attr("disabled", "disabled");
    $.post("../postit.ashx",
                    {
                        msgcontent: $("#msgcontent").val()
                    },
                    function (data) {
                        if (data.indexof('ok') > -1) {
                            alert(data);
                        }
                        else {

 

                            }
                        $("#pwait").hide();
                        $("#btnpost").attr("disabled", "");
                    });
}


在开发的时候,要接受json格式的返回值时,上面的方法貌似不能行,上面的方法貌似接受的是text的文本行。因此,采用jquery的底层ajax实现方法。

 

该方法参数也很多,具体可看帮助文档。本人的常规用法

代码如下:


    function dopostajax(){
            $("#pwait").show();
            $("#btnpost").attr("disabled", "disabled");
            $.ajax({
                url: '../postit.ashx',
                type: 'post',
                datatype: 'json',
                data: { msgcontent: $("#msgcontent").val() },
                timeout: 60000,
                error: function (xmlhttprequest, textstatus, errorthrown) {//请求错误 时执行的方法
                    alert("error!" + errorthrown);
                    $("#pwait").hide();
                    $("#btnpost").attr("disabled", "");
                },
                success: function (data, txtsataus) {//请求成功时执行的方法
                    showcontent(data.content, data.createdate);
                    $("#pwait").hide();
                    $("#btnpost").attr("disabled", "");
                }

 

                });
        }


在ashx代码段,要设置好返回的格式。

 

context.response.contenttype = "application/json";

如果是返回的html或者text的话可以如下写法

context.response.contenttype = "text/plain";

如果ajax方法中设置的返回值是json时,ashx代码返回的格式必须是json格式的数据。
把一个对象转换成json格式,常用方法就是采用开源的第三方类库json.net,newtonsoft.json.dll.

jsonconvert.serializeobject方法就可以转换了。返回json格式后,jquery就可以采用xxx.xxx的方式获取值了。

jsonconvert在处理datetime格式的时候,会返回类似1198908717056的绝对值,因此,在处理datetime的时候,要做一下转换。具体语句如下:

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格式的数据有着天生的处理能力,非常好的兼容json格式数据。

举个例子:

代码如下:


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


这样的代码可以直接写出来,在vs2010的代码编辑器中还可以有代码提示。很强大。

 

ashx完整代码如下:

代码如下:


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;
            }
        }
    }
}


如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网