Cross-origin method recommendation for JQuery _ cross-site, jquery

Source: Internet
Author: User

Cross-origin method recommendation for JQuery _ cross-site, jquery

I found that many bloggers sent text messages asking about cross-origin Javascript. I think many programmers still think that JavaScript cannot be cross-origin. In fact, this is a wrong idea; there are many people looking for solutions on the Internet, and there are a lot of articles on how to use IFRAME to solve the problem. Is it so complicated? In fact, it is very simple. If you use JQUERY, a getjson method will be done, and a line of code will be done.

Paste the method below.

// Cross-domain (across all Domain Names) $. getJSON ("http://user.hnce.com.cn/getregion.aspx? Id = 0 & jsoncallback =? ", Function (json) {// the data format of the remote request page is required :? (Json_data) // For example ://? ([{"_ Name": "Hunan Province", "_ regionId": 134 },{ "_ name": "Beijing", "_ regionId": 143}]) alert (json [0]. _ name );});

Note: getregion. in aspx, when outputting JSON data, you must use Request. queryString ["jsoncallback"] puts the obtained content before the returned JSON data. If the actually obtained value is 42342348, the returned value is 42342348 ([{"_ name ": "Hunan Province", "_ regionId": 134 },{ "_ name": "Beijing", "_ regionId": 143}])

Because the cross-origin principle of getJSON is? Randomly change the method name and return the executed method to achieve cross-origin response.

For more information about getJSON, see the JQUERY manual.

The following is a real example of cross-origin execution:

Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--> <script src = "http://common.jb51.net/script/jquery.js" type = "text/javascript"> </script> <script type = "text/javascript"> // cross-origin (cross all domain name) $. getJSON ("http://e.hnce.com.cn/tools/ajax.aspx? Jsoncallback =? ", {Id: 0, action: 'jobcategoryjson'}, function (json) {alert (json [0]. pid); alert (json [0]. items [0]. _ name) ;}); </script>

The above cross-origin method recommendation for JQuery _ all the content that can be shared across any website is provided by Alibaba Cloud xiaobian. I hope to give you a reference, and I hope you can provide more support to the customer's house.

Related Article

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.