[PhoneGap] XMLHttpRequest Cross-domain issue

819 views
Skip to first unread message

avi

unread,
May 16, 2010, 12:37:04 PM5/16/10
to phonegap
I have read here and everyone seems to say this will not be an issue
since the call will be sent from html loaded via "file:///C".

But I am unable to get the response, tested on chrome, firefox and my
Android device.

Here is the code I am using

function init()
{
callServer();
}
function loadResult(id)
{
var htmlString = "<h2><p>You are loading "+id+"</h2></p>";
document.getElementById("info").innerHTML = htmlString;
}
function callServer()
{
//var url = "http://local.yahooapis.com/LocalSearchService/V3/
localSearch?
appid=0_pxrt_V34GI.anGEEOf09YPEbrhx1vIyo07PxqtqD2CasmYb33WdNemsOIHQYg-
&query=pizza&sort=distance&latitude=%2038.968318&longitude=
%20-77.340515";
var url = "http://www.w3schools.com/xml/note.xml";
req = new XMLHttpRequest();
req.open("GET", url, true);
req.onreadystatechange = onResult;
req.send(null);
}
function onResult()
{
if (req.readyState == 4)
{
loadResult(req.responseText);
}
}

Appreciate your time,
Avi

--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en

For more info on PhoneGap or to download the code go to www.phonegap.com

Giacomo Balli

unread,
May 16, 2010, 4:52:22 PM5/16/10
to phonegap
this is quite a "vague" code so there are many points that could
trigger results that are not the expected ones.
First of all you might want to parse the responseText or responseXML
for just the info you want (doubt you want to include all the headings
etc in a simple div element).
Plus, what is the error? I like to debug the code in safari's
debugging console so that you can see what happens step by step.
> For more options, visit this group athttp://groups.google.com/group/phonegap?hl=en?hl=en

Avinash

unread,
May 16, 2010, 5:54:55 PM5/16/10
to phonegap
Hello Giacomo,

I am sure this is a cross-domain issue because when I download the same xml and saved it in www folder and changed the url ="note.xml" instead of url = "http://www.w3schools.com/xml/note.xml"; it works, the info div loads the xml fine in that scenario.

Also, I am debugging the code using chrome debug tools and firebug. I do not see anything coming back into either responseText or responseXML they are both empty in the onresult method when status==4.

Thank you for your time.
Avinash  

Gilmore

unread,
May 17, 2010, 3:14:30 AM5/17/10
to phonegap
Hi,

What is your webserver saying when the client is making the request?
Do you get at GET call or an OPTIONS call?
It it is the latter you are spot on the money about a XSS issue.

You can quite easily solv this problem by issuing a JSONP request in
stead. Using jQuery 1.3.2+


function loadQuestions(chapter) {
if(!offLine) {
var url = 'http://some.domain.com/some/page/';
$.ajax({
dataType: 'jsonp',
data: 'chapter='+chapter,
jsonp: 'jsonp_callback',
timeout: 5000,
url: url,
success: loadQuestionsCallback,
error: connectionError,
});
}
else {
connectionError();
}
}

function loadQuestionsCallback(data) {
var currentQuestions = eval(data);
currentQuestion = 0;
printQuestion(currentQuestion, currentQuestions);
}


On May 16, 11:54 pm, Avinash <kazaavin...@gmail.com> wrote:
> Hello Giacomo,
>
> I am sure this is a cross-domain issue because when I download the same xml
> and saved it in www folder and changed the url ="note.xml" instead of url =
> "http://www.w3schools.com/xml/note.xml"; it works, the info div loads the
> xml fine in that scenario.
>
> Also, I am debugging the code using chrome debug tools and firebug. I do not
> see anything coming back into either responseText or responseXML they are
> both empty in the onresult method when status==4.
>
> Thank you for your time.
> Avinash
>
> > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscribe@googlegroups.c om>
> > > For more options, visit this group athttp://
> > groups.google.com/group/phonegap?hl=en?hl=en
>
> > > For more info on PhoneGap or to download the code go towww.phonegap.com
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "phonegap" group.
> > To post to this group, send email to phon...@googlegroups.com
> > To unsubscribe from this group, send email to
> > phonegap+u...@googlegroups.com<phonegap%2Bunsubscribe@googlegroups.c om>

Giacomo Balli

unread,
May 17, 2010, 4:42:16 AM5/17/10
to phonegap
some times there are issues regarding the server firing the state and
status.
You might want to set up alerts before your if statements to see what
the server is saying.
> > > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscr...@googlegroups.c om>
> > > > For more options, visit this group athttp://
> > > groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > For more info on PhoneGap or to download the code go towww.phonegap.com
>
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "phonegap" group.
> > > To post to this group, send email to phon...@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > phonegap+u...@googlegroups.com<phonegap%2Bunsubscr...@googlegroups.c om>

Jesse MacFadyen

unread,
May 17, 2010, 4:48:35 AM5/17/10
to Gilmore, phonegap
This looks to be a simple scope issue.
Going back to your original sample post...
The onResult function is added to the req before it exists, unless
there is a "var onResult" definition somewhere else before this.

Sent from my iPhone

Giacomo Balli

unread,
May 17, 2010, 6:36:43 AM5/17/10
to phonegap
if that were the only problem then it wouldn't work with a local
XML... no?


On May 17, 10:48 am, Jesse MacFadyen <jesse.macfad...@nitobi.com>
wrote:
> >>>> %2Bunsubscr...@googlegroups.c om>
> >>>> For more options, visit this group athttp://
> >>> groups.google.com/group/phonegap?hl=en?hl=en
>
> >>>> For more info on PhoneGap or to download the code go towww.phonegap.com
>
> >>> --
> >>> You received this message because you are subscribed to the Google
> >>> Groups "phonegap" group.
> >>> To post to this group, send email to phon...@googlegroups.com
> >>> To unsubscribe from this group, send email to
> >>> phonegap+u...@googlegroups.com<phonegap
> >>> %2Bunsubscr...@googlegroups.c om>
> For more options, visit this group athttp://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go towww.phonegap.com

--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com

Jesse MacFadyen

unread,
May 17, 2010, 1:49:16 PM5/17/10
to Giacomo Balli, phonegap
There could be timing differences with local files, so, it depends.
--
--
Jesse MacFadyen
{
 blog:'blogs.nitobi.com/jesse',
 email:'jesse.m...@nitobi.com',
 company:
 {
   name:'Nitobi Software',
   site:'www.nitobi.com',
   phone:
   {
     office:'+1 (604) 685-9287',
     tollFree:'1-866-632-2777'

Avinash

unread,
May 17, 2010, 3:28:28 PM5/17/10
to Jesse MacFadyen, Giacomo Balli, phonegap
Jesse,

When I debug line by line it is going into all the methods and showing all the alerts in methods. It is just not getting me any response when it is not local.

Giacomo,

I will try the JQuery thing tonight.
But without using JQuery, did anyone use a normal xhr to access a url and load JSON from outside the current domain on an Android device? if so, could you paste your sample code?

Thank you both for your time, 
Avinash

Jesse MacFadyen

unread,
May 17, 2010, 6:40:39 PM5/17/10
to Avinash, Giacomo Balli, phonegap
And when you do it my way, it just works ... verified with both urls (btw, update your API key, it's not private anymore )
Cheers,
  Jesse

/////
req.open("GET", url, true);
req.onreadystatechange = onResult;
req.send(null);
}
function loadResult(id)
{
var htmlString = "<h2><p>You are loading "+id+"</h2></p>";
document.getElementById("info").innerHTML = htmlString;
}
  
// this method is called in the scope of the XHReq
function onResult()
{
if (this.readyState == 4)
{
//alert("onResult!" + this.responseText);
loadResult(this.responseText);
}
}
function onBodyLoad()
{
document.addEventListener("deviceready",onDeviceReady,false);
callServer();

Avinash

unread,
May 18, 2010, 10:32:22 AM5/18/10
to Jesse MacFadyen, Giacomo Balli, phonegap
Dear Jesse,

As soon as you sent the code, I have tested it and it did not work in my Chrome, Firefox or Android device.
So, I came home last night and tested your code on Safari and my iPhone app, it works without any issues, actually my original code also worked without any issues in Safari and iPhone.

So, again did anyone make the XHR call to outside domains work on your Android apps?
Please let me know.

Thank you for your time Jesse,
Avinash
Message has been deleted

Giacomo Balli

unread,
Dec 15, 2010, 6:04:57 PM12/15/10
to phon...@googlegroups.com, Jesse MacFadyen
The cross-domain restriction doesn't apply to PhoneGap.
What is your problem?

Did you try debugging in the browser?

On Dec 15, 2010, at 4:45 PM, EgoAnt wrote:

> Did you ever resolve this issue? I am bumping into the same thing right now. Whenever I try to make a cross-domain XMLHttpRequest from Android I get a blank response.

Erik Maas

unread,
Dec 17, 2010, 3:31:25 AM12/17/10
to phon...@googlegroups.com, Jesse MacFadyen, Giacomo Balli
Don't know why you get that.
I get a normal response from android 2.2 and the android simulator, and the galaxy tab simulator.

I use the ZendJsonRpcClient plugin for jQuery to handle my requests.

2010/12/15 EgoAnt <ego...@gmail.com>
Did you ever resolve this issue? I am bumping into the same thing right now. Whenever I try to make a cross-domain XMLHttpRequest from Android I get a blank response.

--
Reply all
Reply to author
Forward
0 new messages