0

I encounter this problem :

I have a django (python) server that serve XML files on localhost:8000

I have a liferay project using tomcat on localhost:8080, that need to call this XML files (in javascript with ajax)

It call the files, but never get them.

What do I have to do for tomcat to get this file ?

Thanks by advance.

Edit:

here is my request :

$.ajax({
      type: 'GET',
      url: "http://127.0.0.1:8000/charger/entreprise/",
      dataType: "xml",
      crossDomain : true,
      success: function(xml){
            alert(xml);
        }
    });

here is my request header :

Host    127.0.0.1:8000
User-Agent  Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0
Accept  application/json, text/javascript, */*; q=0.01
Accept-Language fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding gzip, deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection  keep-alive
Referer http://localhost:8080/web/guest/home
Origin  http://localhost:8080

In fact, I wonder if the problem come from the javascript, I think its because of the servers.

After trying many solution that didn't work, I finally have solved my problem You guys were right, JSONP was the solution, even if I had to adapt it to django, and my specific problem, which was not easy at all.

BlueMagma
  • 2,392
  • 1
  • 22
  • 46

4 Answers4

2

you have to use JSONP (JSON Padding) to do cross domain AJAX requests. }

Refer to this link for info on JSONP

http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

And jquery also has JSONP feature. Check that out in jquery documentation

Shades88
  • 7,934
  • 22
  • 88
  • 130
  • glad to help. there isn't a JSONP kind of attribute in jquery ajax. but there are ways to do it. search google by jquery ajax cross domain, and u will find plenty links – Shades88 Dec 21 '11 at 18:08
  • Maybe, but I don't want to search anymore, I spend two weeks of my life trying to solve my problem, Know that it's done, I'll do something else ^^ – BlueMagma Dec 22 '11 at 08:19
  • yes indeed. I am glad I could help you in getting some rest :) – Shades88 Dec 22 '11 at 09:35
1

browsers by default block cross domain requests.. if you want to get around this look into JSONP for more details.

Baz1nga
  • 15,485
  • 3
  • 35
  • 61
1

If cross domain ajax is your only problem you can do it with JSONP.

Various JS frameworks have it already implemented (e.g. look for 'crossDomain' setting for JQuery AJAX)

Try to set dataType: "jsonp xml" for your AJAX request.

sbgoran
  • 3,451
  • 2
  • 19
  • 30
  • I have already tried crossDomain settings, but it didn't work – BlueMagma Dec 20 '11 at 10:49
  • Well then maybe you should pass your JQuery AJAX request in question so maybe we can see what is wrong in it. Also check headers you get from server and content type you ask from it via JQuery (they should match). – sbgoran Dec 20 '11 at 11:02
  • I try to set "jsonp xml", and got an error : "missing ; before statement" – BlueMagma Dec 20 '11 at 12:35
  • I set "jsonp; xml" and get back to the previous problem – BlueMagma Dec 20 '11 at 12:37
  • @BlueMagma looks like you've a javascript syntax error "jsonp; xml" is not a valid dataType for jQuery ajax API – Tommaso Barbugli Dec 20 '11 at 12:52
  • Try to set "jsonp text", if you get a result then problem with "jsonp xml" is probably that your xml is not valid, or again some problems with server response headers or somehow JQuery just cant parse your XML. Also try to set error function for AJAX request and see what you can get out of error info. – sbgoran Dec 20 '11 at 13:41
  • I try "jsonp text" and get "missing ; before statement" – BlueMagma Dec 20 '11 at 14:35
  • [object Object] parsererror Error: jQuery171032901807373389824_1324391772633 was not called – BlueMagma Dec 20 '11 at 14:38
  • Hmm, now I googled [this](http://forum.jquery.com/topic/jquery-validation-and-ajax-call-parsererror) and there you have link for the patch at the bottom. Also [here](http://stackoverflow.com/questions/5095307/jquery-json-response-always-triggers-a-parseerror) you have similar if not same discussion. – sbgoran Dec 20 '11 at 14:59
1

As others have mentioned, JSONP will solve your issue. It allows cross-domain AJAX functionality.

An alternative is to use a web server in front of both your Django site and your Tomcat site and have it proxy requests. For example, your main site runs on localhost:80, and based on the URLS, proxies it to the appropriate backend server. This way, from the client's and JavaScript's perspective, the HTML and the JSON requests are going to the same server.

Krystian Cybulski
  • 10,789
  • 12
  • 67
  • 98