JQuery getscript () Usage and cross-domain requests sample

Source: Internet
Author: User
Tags getscript

Load and execute a JavaScript file from the server through an HTTP GET request


jquery.getscript (URL, [Success (data, Textstatus)])

URL a URL string containing the request sent.

Success (data, textstatus) a callback function that executes when a request succeeds.

This is a fast AJAX processing function, which is equivalent to:

The code is as follows Copy Code

$.ajax ({
Url:url,
DataType: ' Script ',
Success:success
});

Callback by a file that returns JavaScript. Usually not useful as the script has been executed to this point.

This script has been executed in the global environment, so point to other variables and use the jquery function. The included script must have some effect on the current page:

The code is as follows Copy Code

$ ('. Result '). html (' <p>lorem ipsum dolor sit amet.</p> ');

By referencing this file name, the script is included and executed:

The code is as follows Copy Code

$.getscript (' Ajax/test.js ', function () {
Alert (' Load was performed. ');
});


Jquery.getscript () cross-domain request

The code is as follows Copy Code


Can implement Cross-domain requests to get remote resources

$.getscript ("http://www.vipinit.com/test.php", function (data, textstatus) {
Console.log (data); Data returned
Console.log (Textstatus); Success
Console.log ("Load was performed.");
});

Test.php need to return a JSON result.
You can also use this method:

The code is as follows Copy Code

$.ajax ({
Url:url,
DataType: "Script",
Success:success
});

Jquery.getscript cannot set the encoding type, $.ajax can make up for the

The code is as follows Copy Code
$.ajax ({
Url:url,
DataType: "Script",
Scriptcharset: "gb2312",
Success:success
});

In addition to resolving cross-domain problems, you can also load remote scripts through the script label's SRC:

The code is as follows Copy Code
<script src= "test.php" type= "Text/javascript" ></script>

or open a new page under this domain to make a remote request, return the data to the domain under the access

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.