Home  >  Article  >  Web Front-end  >  Summary of usage of jquery ajax, ashx, json_jquery

Summary of usage of jquery ajax, ashx, json_jquery

WBOY
WBOYOriginal
2016-05-16 17:00:421095browse

The simplified version of the ajax calling method provided by jquery is usually as follows:

Copy code The code is as follows:

function post() {
$("#divWait").show();
$("#btnPost").attr("disabled", "disabled");
$. post("../PostIt.ashx",
                                                                                                      function (data) {
.indexof ('ok') & gt; -1) {
Alert (data);
}
Else {

                                                                                       disabled", "");
                                                              


During development, when it is necessary to accept the return value in json format, the above method does not seem to work. The above method seems to accept text lines of text. Therefore, the underlying Ajax implementation method of jQuery is adopted.

This method also has many parameters, please see the help document for details. My regular usage


Copy code


The code is as follows:

function doPostAjax(){
$("#divWait").show();
$("#btnPost").attr("disabled", "disabled");
            $.ajax({
                                                                                                                                                                                                                       .          data: { msgContent: $("# msgContent").val() },
timeout: 60000,
error: function (XMLHttpRequest, textStatus, errorThrown) {//Method executed when request error
alert("error!" errorThrown);
                                                                                                                                Success: function (data, txtSataus) {//Method executed when the request is successful ("#btnPost") .attr("disabled", "");
                                                                                                      
                  });

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

If it is returned html or text, it can be written as follows

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

If the return value set in the ajax method is json, the format returned by the ashx code must be json format data. To convert an object into json format, the common method is to use the open source third-party class library json.net, Newtonsoft.Json.dll.


The JsonConvert.SerializeObject method can be used for conversion. After returning the json format, jquery can use XXX.xxx to obtain the value.

JsonConvert will return an absolute value similar to 1198908717056 when processing datetime format. Therefore, when processing datetime, some conversion is required. The specific sentences are as follows:
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();  

//Use a custom date format here, if not used, the default is ISO8601 format   'HH':'mm':'ss";

string output = JsonConvert.SerializeObject(m, Newtonsoft.Json.Formatting.Indented, timeConverter);

By the way, javascript has a natural ability to process json format data and is very compatible with json format data.

For example:


Copy code

The code is as follows:


function pppp() {

       var person = { "name": "jack", "age": 24, "sex": true };

            alert(person.name); >           alert(person.sex);
Such code can be written directly, and there can also be code prompts in the vs2010 code editor. Very powerful.

The complete code of ashx is as follows:

Copy the code The code is as follows:

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

    namespace nnn
{
    ///


    /// PostIt 的摘要说明
    ///

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn