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!

Mark
  • 422
  • 1
  • 5
  • 15

2 Answers2

1

The ports differ so your server (request target server) need to allow cross-domain requests.
Just send this header:

Access-Control-Allow-Origin: *

E.g. for PHP ( http://enable-cors.org/server_php.html ):

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

Or use .htaccess ( http://enable-cors.org/server_apache.html ):

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

More infos and how to: http://enable-cors.org/server.html

You can also use JSONP. Sample here (may help): JSONP web service with python

Community
  • 1
  • 1
fnkr
  • 9,428
  • 6
  • 54
  • 61
  • thank you very much, I add "" in page B, and I would get a "parsererror" if I crawl page B, what should I do? ps. the page B is just a simple html, no js, no css. – Mark Oct 14 '13 at 09:24
  • You need to add the `Access-Control-Allow-Origin` to HTTP header not to HTML header. – fnkr Oct 14 '13 at 09:26
  • I add it in ajax as described above, but it still does not work. – Mark Oct 14 '13 at 09:38
  • You need to add it to your server, not to the javascript code. What server are you using? – fnkr Oct 14 '13 at 09:41
  • there are only two pages(A and B), which are written by django + jquery. the server is django? – Mark Oct 14 '13 at 09:46
  • The application running on 127.0.0.1:8082 that replies to your browser with the HTML code. – fnkr Oct 14 '13 at 09:48
  • Yes, django is your server. This may help you: http://stackoverflow.com/questions/14377050/custom-http-header-in-django – fnkr Oct 14 '13 at 09:54
0

You cannot do that because of the same source policy. Page A and page B are from different domains since the port number differs.

Check Jsonp: http://en.wikipedia.org/wiki/JSONP which maybe the solution.

Rami
  • 7,162
  • 1
  • 22
  • 19