jquery cross-domain access problem Resolution (notes) _jquery

Source: Internet
Author: User
These two days need to achieve a level three domain name direct URL rewrite to the site static page, such as Http://123.456.789.com/UrlRewrite to http://www.789.com/news/123.html

Speaking of this, perhaps and JS Cross-domain Access does not have a bit of a relationship, in the mind of the arrangement of this problem is related to Urlrewrite. All right, now urlrewrite. All ready, directly in the browser address bar input http://123.456.789.com/will find that this in the address bar directly through the http://www.789.com/news/123. HTML to access the page has an exception, the page image and style and JS seems to have lost its role.

Take a look at the URL in the two address bar and suddenly pat yourself on the head, a bit of a train of thought. Originally this page source code in the picture, CSS, JS are through the relative path to request. Of course, this is not a problem in direct access to the http://www.789.com/news/123.html path, but when you use http://123.456.789.com/access to see the address bar two URL obvious path is completely different, For example, images/1.gif in 1 is equivalent to request http://www.789.com/news/1.gif and then 2 to become what yun_qi_img/1.gif it is obvious that this picture you will never request. Because you will never find this image in this path. Okay, in order to be simple, we do not use the Internet to introduce many kinds of methods, the absolute path to solve it. OK, everything looks so natural, so carefree. But the real trouble is still behind.

It's not like we're heading all the time here. Don't worry, the problem is always solved. A second one emerges.

First of all, tell me about this page, this page is through the program will be dynamically presented things static, but the static page inside and must show some changes in the information, here many people may think of using IFRAME to solve, but such a solution is not what I want to use, 1. IFRAME is less efficient than other methods and 2.IFRAME is unfriendly to crawling spiders. So the first thing I'm going to do here is to use the jquery load function to simply request a dynamic page and then display the returned dynamic information in a static page. Well, there may be some friends who already know why this diary is called "jquery cross-domain access Problem".

First look at my parameter $ ("#head") in the jquery load function. Load ("http://www.cnblogs.com/project/ajax.php" {"Action": "Head"}); Of course the argument has been replaced with $ ("#head"). Load ("http://www.789.com/project/ajax.php" {"Action": "Head"}); But there is still a "jquery.js 138 lines do not have access to" directly run to see the juqery138 line to coax the original load function here is a problem (did not read the source code, to tell the truth too tortured, a heap of compressed JS code to see people want to die) Well, go online Google, Several people's answers caught my attention. They all say this is a problem with JS Cross-domain operation. JS is not able to operate directly across domains. And then contact their own three-level domain name Urlrewrite again to think about cross-domain problem, three domain name under the domain is 789 under 456 123 and itself requested 789.com domain name so this is not cross-domain what is it? Enlightened. I'm sure I found the problem.

Continue to Google, a lot of people recommend jquery Getjson use callback function to solve the principle of such a solution and then slowly study, OH continue to search http://blog.ossxp.com/2010/02/462/This article is very good to give me a lot of help, Directly in their own projects or according to gourd painting ladle. Problem solved!

HTML page jquery code
Copy Code code as follows:

$.getjson ("http://www.789.com/project/ajax.php?Action=head&callback=?"),
function (data) {//Back table passed parameter 1;
var html = decodeURI (DATA.STR)
$ (' #head '). HTML (HTML); Invoke the div to display the content
});

Server-side PHP code
Copy Code code as follows:

$str = "<ul>
This is test
</ul> ";
$arr [' str '] = $STR;
$json = Json_encode ($arr); Using the Json_encode function to process an array of PHP
Echo $_get[' callback ']. " (". $json.") ";

To this through the level three domain name urlrewrite to static page Cross-domain Access server dynamic class capacity problem solved successfully!

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.