Fórum

XMLHttpRequest

Jimmy Huguet, modificado 12 Anos atrás.

XMLHttpRequest

New Member Postagens: 11 Data de Entrada: 13/12/11 Postagens Recentes
Hi,

I'm a beginner with liferay, and I run into this problem :

I'm working with google map API, and I receive an XML file in JavaScript with an XmlHttpRequest.

Everything goes well in a regular HTML page, but with the same code used in a portlet,

XmlHttpRequest.responseXML is always null.

Do you know why ?

Do you have any solution to get the XML or solve this problem ?

Thank by advance for your reaction.

PS : I'm french and I apologize about my english..emoticon
thumbnail
Ravi Kumar Gupta, modificado 12 Anos atrás.

RE: XMLHttpRequest

Liferay Legend Postagens: 1302 Data de Entrada: 24/06/09 Postagens Recentes
Which browser you are testing.. xmlhttprequest will not be there in IE.. Rather use jquery like library and use ajax using that..
Jimmy Huguet, modificado 12 Anos atrás.

RE: XMLHttpRequest

New Member Postagens: 11 Data de Entrada: 13/12/11 Postagens Recentes
I use Firefox,

I'm not sure about that, but as i need to go throught the XML, so I can't use JQuery.

What do you mean "use ajax", XMLHttpRequest isn't already ajax ?
thumbnail
Ravi Kumar Gupta, modificado 12 Anos atrás.

RE: XMLHttpRequest

Liferay Legend Postagens: 1302 Data de Entrada: 24/06/09 Postagens Recentes
Yep.. thats ajax.. i was just saying that when we use such smart library we dont need to be worried about many things..

And to work on xml jquery works fine.. http://api.jquery.com/jQuery.parseXML/
A sample code

$.ajax({
  type: 'GET',
  url: "foo.aspx",
  data: {
    key: "value"
  },
  dataType: "xml",
  success: function(xml){
    var clientid = $(xml).find('client_id').eq(1).text();
    alert(clientid);
  }   
});


Anyway, here question is about why xmlResponse is null.. can you check the url response... the url you are requesting.. just call that in browser and see.
Jimmy Huguet, modificado 12 Anos atrás.

RE: XMLHttpRequest

New Member Postagens: 11 Data de Entrada: 13/12/11 Postagens Recentes
Thank you for your answer,

I try this code (with JQuery), still, without liferay it works, but with liferay, I got nothing because it never enter the success function.

When I enter the exact url use in javascript in my browser, I get the XML I want to parse.

And I know the request work, because the XML is generate in a django application, and the server that serve the XML file told me he receive the request and respond.

Also, with XMLHttpRequest, when I reached the "readyState" 4, the status is 200.emoticon

That's why it's so weird..emoticon
Jimmy Huguet, modificado 12 Anos atrás.

RE: XMLHttpRequest

New Member Postagens: 11 Data de Entrada: 13/12/11 Postagens Recentes
Sorry, I made a mistake, the status still stay at 0 even when reached the 4th state emoticon
thumbnail
Ravi Kumar Gupta, modificado 12 Anos atrás.

RE: XMLHttpRequest

Liferay Legend Postagens: 1302 Data de Entrada: 24/06/09 Postagens Recentes
Can you please try removing datatype:xml once and see what's coming..

Also,
With firebug, check if the request is going proper.. check the parameters its sending and the response..
Jimmy Huguet, modificado 12 Anos atrás.

RE: XMLHttpRequest

New Member Postagens: 11 Data de Entrada: 13/12/11 Postagens Recentes
I use this code :

$.ajax({
type: "GET",
url: url,
dataType : "xml",
success: function(xml)
{
alert($(xml));
}
});


Without liferay :
Everything is ok with firebug, I got the file.

With liferay :
response : ""
XML : "Erreur d'analyse XML : aucun élément trouvé Emplacement : moz-nullprincipal:{538eb746-12a3-42ad-93e8-ff72a9c733d4} Numéro de ligne 1, Colonne 1 :"

Here the heade :

Réponse
Date Mon, 19 Dec 2011 07:32:37 GMT
Server WSGIServer/0.1 Python/2.6.5
Last-Modified Mon, 19 Dec 2011 07:32:37 GMT
Expires Mon, 19 Dec 2011 07:32:42 GMT
Content-Type text/xml
Cache-Control max-age=5
Requête
Host 127.0.0.1:8000
User-Agent Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0
Accept application/xml, text/xml, */*; 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




The same when I remove "dataType:"xml"
Jimmy Huguet, modificado 12 Anos atrás.

RE: XMLHttpRequest

New Member Postagens: 11 Data de Entrada: 13/12/11 Postagens Recentes
I have news :

Apparently, the problem is server side, tomcat does not accept cross domain ajax request, and that's why it does not work.

Now my question is : "How could I solve this ?"
thumbnail
Ravi Kumar Gupta, modificado 12 Anos atrás.

RE: XMLHttpRequest

Liferay Legend Postagens: 1302 Data de Entrada: 24/06/09 Postagens Recentes
Did you search google for "Cross Domain Ajax Tomcat".. seems like there are some solutions available..

I am not sure which one will work... but you can give them a try.. emoticon
http://33roundwood.blogspot.com/2009/12/ajax-cross-domain.html
http://www.servletsuite.com/servlets/crossdomainajaxflt.htm
Jimmy Huguet, modificado 12 Anos atrás.

RE: XMLHttpRequest

New Member Postagens: 11 Data de Entrada: 13/12/11 Postagens Recentes
Thank you, I tried the servlet solution, it still does not work, but I'm confident.

And I feel I'm moving forward, that what hell of a problem.

thank you again for your support.

I'll post the solution I use as soon as it work emoticon
Jimmy Huguet, modificado 12 Anos atrás.

RE: XMLHttpRequest

New Member Postagens: 11 Data de Entrada: 13/12/11 Postagens Recentes
Here is finally the solution that worked for me, in 1 word : JSONP

I had to work from the the side of django (the serving server), instead of serving an XML file, I serve a JS file in wich I parse my XML as a string

I no longer use XMLHttpRequest, instead I add a script tag in the head of my page (in javascript), with the url that is serving above

And it totally Work !

The problem was related to SOP : Same Origin Policy

If you have the same (or close enough) problem than me THIS is the solution.
Tony Turner, modificado 11 Anos atrás.

RE: XMLHttpRequest

New Member Postagens: 4 Data de Entrada: 31/05/12 Postagens Recentes
I know this is an old post, but how exactly do you implement a remote JSONP service using Liferay Service Builder?

If I add the @JSONWebService annotation then SB will create and return a JSON web service.

Do I need to implement my own JSONP service as a servlet using something like Resteasy?

Cheers