1

I have two pages (A and B), which are written by using django + jquery. and their urls are:

http://127.0.0.1:8081/temp1/ (pageA)
http://127.0.0.1:8082/temp2/ (pageB)

I want to crawl page B in page A's js script by using ajax(), and the js script is:

 $("#tmp_button1").click(function(tmp_event) {
    $.ajax({
            url: "http://127.0.0.1:8082/temp2/",
            async: false,
            headers: {
                   "Access-Control-Allow-Origin" : "*"
            },
            error: function(request, error) {
                    alert(error);
            },
            success: function(response) {
                    alert(response);
            }
    });
});

unfortunately, I could not crawl the page B. is this a cross-domain action? if I want to crawl page B by using jquery, what should I do?

any help would be appreciated!

4

2 回答 2

1

端口不同,因此您的服务器(请求目标服务器)需要允许跨域请求。
只需发送此标头:

Access-Control-Allow-Origin: *

例如对于 PHP ( http://enable-cors.org/server_php.html ):

 header("Access-Control-Allow-Origin: *");

或使用 .htaccess ( http://enable-cors.org/server_apache.html ):

Header set Access-Control-Allow-Origin "*"

更多信息和如何:http ://enable-cors.org/server.html

您还可以使用JSONP。此处示例(可能有帮助):JSONP web service with python

于 2013-10-14T09:09:02.970 回答
0

由于相同的源策略,您不能这样做。由于端口号不同,页面A和页面B来自不同的域。

检查 Jsonp:http ://en.wikipedia.org/wiki/JSONP这可能是解决方案。

于 2013-10-14T09:10:41.067 回答