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.