Summary of JSON and JSONP, and summary of JSONJSONP
1. What is json
JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. It is based on a subset of JavaScript Programming Language, Standard ECMA-262 3rd Edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language.
2. What is JSONP?
JSONP (JSON with Padding) is a "usage mode" in JSON format that allows webpages to request data from other domains. Another new solution to this problem is cross-source resource sharing.
JSONP
Definition: A Cross-Origin data access method.
Function and principle: after the client declares the callback function, the client requests data to the server through the script tag, and then the server returns the corresponding JSON data and dynamically executes the callback function.
Use the following in jQuery:
// JQuery JSONP Support var url = "http: // 192.168.0.103: 12075/api/test. aspx? Id = 1 & callback =? "; JQuery. getJSON (url, function (data) {// process the returned json data alert ("name:" + data. name + ", Price:" + data. price );});
Use the following to call webservices:
$. GetJSON ("http: // 192.168.0.103: 12050/services/WebService1.asmx/ws/test? Callback =? ", {Name:" jim ", time:" 2 "}, function (data) {// process the returned json data alert (decodeURI (data. msg ));});
Server (webservices background code ):
[WebMethod] public void ws (string name, string time) {HttpRequest Request = HttpContext. current. request; string callback = Request ["callback"]; HttpResponse Response = HttpContext. current. response; Response. write (callback + "({msg: 'Return Data '})"); Response. end ();}
Summary JSONP:
Note:
1. error handling mechanism is not provided, data is returned, and no content is displayed. An Error 404 is returned.
2. If you use untrusted services, it will pose a great security risk.
JSON
JSON is a lightweight data exchange format compared with XML. Easy to read and write. At the same time, it is easy to parse and generate machines, which can be divided into two types: Objects (using the lower point syntax to obtain attribute values) and arrays (traversing arrays and obtaining indexes ).
1. Convert JSON text into JavaScript objects
Read JSON data from the web server, convert JSON data into JavaScript objects, and then use the data on the web page.
The JavaScript function eval () can be used to convert JSON text to JavaScript objects. Text must be enclosed in parentheses to avoid syntax errors.
That is, you can use the eval function to convert a json string to a json object. Then, you can access the json string in two ways:
Server:
// User object, which is serialized in the background for the foreground to use string strJson = Newtonsoft. Json. JsonConvert. SerializeObject (user); context. Response. Write (strJson );
Front-end:
$. GetJSON ("webdata/test. aspx ", function (data) {// get a value (username: User Name) in the object $ (" # divmessage "). text (data. username );});
2. array acquisition
packJson = [{"name":"nikita", "password":"1111"},{"name":"tony", "password":"2222"}];
Loop traversal:
for(var i = 0; i < packJson.length; i++){alert(packJson[i].name + " " + packJson[i].password);}
The above is a simple summary of JSON and JSONP introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!