Ajax--getscript ()/getjson () method in jquery source code

Source: Internet
Author: User
Tags getscript

One, $.getscript () method

    • Sometimes it is completely unnecessary to get all the JavaScript files you need when the page is first loaded, and can be taken as needed.
    • This function is used to dynamically load the JS file and execute the JS code in the file at the global scope.
    • This function can load a cross-domain JS file. Note that the function is loading data asynchronously .
    • The function belongs to the global jQuery object.

Grammar:

$ (function() {        $ ("send"). On ("click",function() {            $.getscript (" Script.js ",function() {                //// callback               //data: returned data  ///Textstatus: A string describing the status             }             );});     

Source code for the $.getscript () method:

function (URL, callback) {        return jquery.get (url, undefined, callback, "script" );    }

In this method, the $.get () method is called, and the parameters of the response are passed in.

The syntax structure of the $.get () method is as follows:

Jquery.get (URL [, data] [, success] [, type])

The $.get () method is equivalent to:

"GET""script"});

Second, $.getjson () method

    • This method is used to load the JSON file
    • If the URL of the request includes "callback=?" And so on, jquery automatically sees it as a JSONP and executes the corresponding callback function to get the JSON data.
    • The function is to load the data asynchronously .
    • The function belongs to the global jQuery object.

Grammar:

$ (function() {        $ ("send"). On ("click",function() {            $.getjson (" Test.json ",function(data) {                /// callback function
Data: Returned
Textstatus: A string describing the status of } );});

Source code for the $.getjson () method:

function (URL, data, callback) {        return jquery.get (URL, data, callback, "JSON" );    }

In this method, the $.get () method is also called, and the corresponding parameters are passed in.

$.getjson () is equivalent to:

"GET""JSON"});

Both the *$.getscript () and $.getjson () methods are implemented through the encapsulation of the $.get () method. Belongs to the third tier method.

In short

Third floor $.getjson (), $.getscript ()
Second floor $.get (), $.post ()
First floor $.ajax ()

Ajax--getscript ()/getjson () method in jquery source code

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.