JQuery_review: $. getScript () and $. getJSON are used to perform asynchronous information interaction with the server. jquerygetscript

Source: Internet
Author: User
Tags getscript

JQuery_review: $. getScript () and $. getJSON are used to perform asynchronous information interaction with the server. jquerygetscript
JQuery also provides two very practical methods: $. getScript () and $. getJSON (). These two methods are very important. Suppose we want to write a framework. What should we do if we can control the actions of the foreground table in the background? It is certainly necessary to send a JavaScript code to the front end in the background to execute the JavaScript code on the front end. Of course, there are many methods, such as creating a Script node and placing it in the DOM tree, or add a <script> node to HTML to solve this problem. However, getScript is also a good case to solve this problem.
In addition, JSON is easy to write, data transmission is relatively small, data redundancy is good, and the XML format is gradually replaced. Google also provides reliable json conversion solutions for many third parties. Therefore, the power of json cannot be underestimated.
There are several points to note when using jQuery's two solutions: These two methods $. getScript () and $. getJSON () has two parameters. The first parameter is to provide jQuery with the address to seek resources, and the second parameter is what functions will be provided after the resource is returned to call the above resources, the first callback function is used to execute the functions in the preceding JavaScript method, and the second callback function is used to parse the JSON string just obtained. In addition, the JSON format is {"name1": "value1", "name2": "value2"}. Note that it is not a single quotation mark, but a double quotation mark, this may cause jquery to fail to parse the above JSON string. Finally, how do I parse JSON? Data ['name1'] and use $. each (data, function (idx, item) {}) for parsing. Select the correct resolution method based on the JSON format.
The front-end DEMO code is as follows:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


The backend DEMO code is as follows:
@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {String name = req.getParameter("name");String address = req.getParameter("address");StringBuffer sb = new StringBuffer("");sb.append("{\"name\":\"")  .append(name)  .append("_czz\",\"address\":\"")  .append(address)  .append("_czz\"}");resp.setContentType("html/JSON;charset=utf-8");resp.getWriter().print(sb.toString());}


The JS Code called in the program is as follows:
function alertHtml(e){alert(e.html());}




$ GetJSON () of jquery

What about cache? 'getdate. do? Id = 100 '. You can just upload a different object each time, like this:
'Getdate. do? Id = 100 & t = '+ Math. random ()
It uses a random number or a new Date () timestamp.

Why is the script first executed for programs outside $ getJSON, instead of those in $ getJSON? Solution!

Function Msg (){
$. GetJSON (url, function (data) {// first run to this
Alert ("coming! "); // Call the callback function after getJSON is obtained.
});
Alert ("float! "); // Direct alert
}
The process is like this. A process is required to retrieve the json URL. However, I will not wait for this JSON. So it pops up first. After obtaining the JSON. Trigger the callback function. This is "coming.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.