Jquery's ajax asynchronous request receives the returned json data instance

  • 2020-03-30 03:20:06
  • OfStack

Jquery's ajax asynchronous request receive return json data method is simple to set, One is that the server handler returns json data, and the other is that the datatype of the ajax send setting is set to jsonp format data or json format Will do.

The code example is as follows:


$('#send').click(function () {
    $.ajax({
        type : "GET",
        url : "a.php",
        dataType : "jsonp",
        success : function (data) {
            $.each(data.items, function (i, item) {
                $("<img class='para'/> ").attr("src", item.media.m).appendTo("#resText");
                if (i == 3) {
                    return false;
                }
            });
        }
    });
});

The ajax method is as follows:


$.ajax({
   type: "POST",
   url:  ctxRoot+'FolderAction!saveInformSetting.action',
   data: 'jsonStr=' + inform_settingListStr,
   dataType: "json",
   complete: function(data){
       //I'm going to do something here, and I'm going to assume that the returned json data has the property name in it
       //Sometimes you can access
directly with data.name or data['name']        //Var jsonData = eval("("+ data.responsetext +")"); Can be accessed through jsondata.name, and in this case needs to be complete instead of success
   }
   });
$.ajax(options)

This is the basic JQuery Ajax method, with only one option, which contains the request information and the callback function information. The parameter contents are all in the form of key:value pairs and are all optional.

The syntax is as follows:

$. Ajax ({options});
Url :(string) the address to which the request is sent, either a server page or a WebService action.
Type :(string) request mode, POST or GET
Data :(object) the data that is sent to the server when a request is sent. {name:"grayworm",sex:"male"}, if array {works:["work1","work2"]}
DataType :(string) the dataType expected to be returned. XML, HTML, json, text, etc
BeforeSend :(Function) is triggered before sending the ajax request, and is canceled if it returns false. If an asynchronous request needs to display a GIF animation, it should be set here. Img> The visible.

PS: here again for you to recommend a few more practical json online tools for your reference:

Online JSON code verification, verification, beautification, formatting tools:
(link: http://tools.jb51.net/code/json)

JSON online formatting tool:
(link: http://tools.jb51.net/code/jsonformat)

Online XML/JSON interconversion tool:
(link: http://tools.jb51.net/code/xmljson)

Json code online formatting/beautification/compression/editing/conversion tools:
(link: http://tools.jb51.net/code/jsoncodeformat)

Online json compression/escape tool:

(link: http://tools.jb51.net/code/json_yasuo_trans)

C language style /HTML/CSS/json code format beautification tool:
(link: http://tools.jb51.net/code/ccode_html_css_json)


Related articles: