Getjson () This is what I'm going to talk about today. jquery Operation JSON data function
Jquery.getjson (URL, [data,] [Success (data, Textstatus, JQXHR)]
Urla string containing the URL to which the request is sent.
Dataa map or string is sent to the server with the request.
Success (data, Textstatus, JQXHR) A callback function This is executed if the request succeeds.
Three parameters are accepted in the callback function, the first book returns the data, the second is the state, and the third is the XMLHttpRequest of jquery, we only use the first parameter.
$.each () is the method used to parse JSON data in a callback function, the following is an official document:
Jquery.each (collection, Callback (Indexinarray, valueofelement))
Collectionthe object or array to iterate over.
Callback (Indexinarray, valueofelement) The function that'll be executed on every object
Use AJAX requests to get JSON data and output the results:
The code is as follows |
Copy Code |
$ ("button"). Click (function () { $.getjson ("Demo_ajax_json.js", function (Result) { $.each (result, function (i, field) { $ ("div"). append (Field + ""); }); }); }); |
Cases
Load JSON data from Test.js and display a name field data from the JSON data:
The code is as follows |
Copy Code |
$.getjson ("Test.js", function (JSON) { Alert ("JSON Data:" + json.users[3].name); }); |
$.getjson () Cross-domain request
1, the same domain name and other requests can be the same
Js:
The code is as follows |
Copy Code |
var url= "Http://localhost:2589/a.ashx"; $ (function () { $.getjson (url,function (data) { Alert (data. Name); }) }); |
Server return string:
{' Name ': ' Loogn ', ' age ': 23}
2, under different domain names
Js:
The code is as follows |
Copy Code |
var url= "http://localhost:2589/a.ashx?callback="; $ (function () { $.getjson (url,function (data) { Alert (data. Name); }) }); |
Server return string:
jquery1706543070425920333_1324445763158 ({"Name": "Loogn", "Age": 23})
The returned string is a function called "jquery1706543070425920333_1324445763158", and the argument is {"Name": "Loogn", "Age": 23}.
In fact, this very long function name is the role of callback= in the request path, I think it should be: The $.getjson method generates a reference to the callback method name, replace? The request above will become
http://localhost:2589/a.ashx?callback=jQuery1706543070425920333_1324445763158&_= 1324445763194, the server returns JSON to be processed, such as:
The code is as follows |
Copy Code |
String cb = Context. Request["Callback"]; Context. Response.Write (CB + "(" + JSON +) "); |
Parameter name callback can also be replaced by Jsoncallback, I think is afraid of conflict, jsoncallback should be priority detection, no further detection callback (no test!!) )
? is also a specific function name, so that the callback function can not be anonymous, with? The build is just a convenience that jquery provides for our general operations.
Chinese character parameter garbled problem.
When this happens, first look at the Web server's encoding. For example: Using Tomcat, you can view
Conf/server.xml file in the <Connector/> tag is not set uriencoding, if there is a conversion based on this code, if not this default to "ISO-8059."
If uriencoding= "GBK", the background code is as follows:
Java code
The code is as follows |
Copy Code |
1. String test = request.getparameter ("test"); Chinese characters that come back from the front 2. Test = new String (test. GetBytes ("GBK"), "Utf-8"); Turn into UTF-8 format String test = request.getparameter ("test"); Chinese characters that come back from the front Test = new String (test. GetBytes ("GBK"), "Utf-8"); Turn into UTF-8 format |
Note: ' GBK ', ' utf-8 ' to lowercase
Java code
Workaround 1:
The code is as follows |
Copy Code |
The front desk revision should read var data = {Name:encodeuri ($ ("#myName"). Val (), "Utf-8"), pwd: "Password"}; Background revision changed to String Name=urldecoder.decode (Request.getparameter ("name"), "Utf-8"); |
Workaround 2:
code is as follows |
copy code |
Foreground is modified to var data = {Name:encodeuri ($ ("#myName"). Val (), "Utf-8"), pwd: "Password"}; Background is modified to String Name=urldecoder.decode (request.getparameter ("name"), "Utf-8"); |