Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Jimmy Huguet
XMLHttpRequest
December 15, 2011 4:38 AM
Answer

Jimmy Huguet

Rank: New Member

Posts: 11

Join Date: December 13, 2011

Recent Posts

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
Ravi Kumar Gupta
RE: XMLHttpRequest
December 15, 2011 6:34 AM
Answer

Ravi Kumar Gupta

Rank: Liferay Legend

Posts: 1146

Join Date: June 23, 2009

Recent Posts

Which browser you are testing.. xmlhttprequest will not be there in IE.. Rather use jquery like library and use ajax using that..
Jimmy Huguet
RE: XMLHttpRequest
December 15, 2011 6:58 AM
Answer

Jimmy Huguet

Rank: New Member

Posts: 11

Join Date: December 13, 2011

Recent Posts

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 ?
Ravi Kumar Gupta
RE: XMLHttpRequest
December 15, 2011 8:51 AM
Answer

Ravi Kumar Gupta

Rank: Liferay Legend

Posts: 1146

Join Date: June 23, 2009

Recent Posts

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
 1
 2$.ajax({
 3  type: 'GET',
 4  url: "foo.aspx",
 5  data: {
 6    key: "value"
 7  },
 8  dataType: "xml",
 9  success: function(xml){
10    var clientid = $(xml).find('client_id').eq(1).text();
11    alert(clientid);
12  }   
13});


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
RE: XMLHttpRequest
December 16, 2011 12:23 AM
Answer

Jimmy Huguet

Rank: New Member

Posts: 11

Join Date: December 13, 2011

Recent Posts

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
RE: XMLHttpRequest
December 16, 2011 6:39 AM
Answer

Jimmy Huguet

Rank: New Member

Posts: 11

Join Date: December 13, 2011

Recent Posts

Sorry, I made a mistake, the status still stay at 0 even when reached the 4th state emoticon
Ravi Kumar Gupta
RE: XMLHttpRequest
December 16, 2011 9:08 AM
Answer

Ravi Kumar Gupta

Rank: Liferay Legend

Posts: 1146

Join Date: June 23, 2009

Recent Posts

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
RE: XMLHttpRequest
December 18, 2011 11:43 PM
Answer

Jimmy Huguet

Rank: New Member

Posts: 11

Join Date: December 13, 2011

Recent Posts

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
RE: XMLHttpRequest
December 19, 2011 1:47 AM
Answer

Jimmy Huguet

Rank: New Member

Posts: 11

Join Date: December 13, 2011

Recent Posts

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 ?"
Ravi Kumar Gupta
RE: XMLHttpRequest
December 19, 2011 2:40 AM
Answer

Ravi Kumar Gupta

Rank: Liferay Legend

Posts: 1146

Join Date: June 23, 2009

Recent Posts

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
RE: XMLHttpRequest
December 19, 2011 5:27 AM
Answer

Jimmy Huguet

Rank: New Member

Posts: 11

Join Date: December 13, 2011

Recent Posts

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
RE: XMLHttpRequest
December 21, 2011 7:44 AM
Answer

Jimmy Huguet

Rank: New Member

Posts: 11

Join Date: December 13, 2011

Recent Posts

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
RE: XMLHttpRequest
November 19, 2012 3:21 AM
Answer

Tony Turner

Rank: New Member

Posts: 6

Join Date: May 31, 2012

Recent Posts

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