/    Sign up×
Community /Pin to ProfileBookmark

Status code 0

Hi,

I’m trying to get some data through a PHP file in AJAX. It works
really fine, but only on Desktop. When i go mobile (iPhone 4, Android)
it doesnt work. I’ve got a status = 0 back and my responseText is
empty.

It goes about the following code (with a callback).

[CODE]function loadXML(method, url, onsuccess)
{

var xmlhttp;

if (window.XMLHttpRequest)
{ // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{ // code for IE6, IE5
xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}

xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
onsuccess(xmlhttp.responseText);
}

if(xmlhttp.readyState == 4 && xmlhttp.status == 0)
{
onsuccess(xmlhttp.responseText);
}
}

xmlhttp.open(method, url, true);
xmlhttp.send(null);

}

function initialize() {

loadXML(“GET”, “http://example.com/testing.php”,
function(location) {
//Here is the variabele
location empty (only on Android/iPhone).
});
}[/CODE]

When i test my script on a browser, it gives me the right data back.
On Android/iPhone, it gives nothing back. Knows anybody the problem
and how i can solved it?

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 10.2011 — There something wrong within this part. There's a mess:
<i>
</i>function initialize() {

<i> </i> loadXML("GET", "http://example.com/testing.php",
function(location) {
//Here is the variabele
location empty (only on Android/iPhone).
});
}

It is not very clear where the functions start and end...
Copy linkTweet thisAlerts:
@LeanderauthorMay 10.2011 — Maybe this version is better?

[CODE]
function loadXML(method, url, onsuccess)
{
var xmlhttp;
if (window.XMLHttpRequest)
{ // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{ // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
onsuccess(xmlhttp.responseText);
}

if(xmlhttp.readyState == 4 && xmlhttp.status == 0)
{
onsuccess(xmlhttp.responseText);
}
}

xmlhttp.open(method, url, true);
xmlhttp.send(null);

}

function initialize() {
loadXML("GET", "http://example.com/testing.php", function(location) {
//Here is the variabele location empty (only on Android/iPhone).
});
}
[/CODE]
Copy linkTweet thisAlerts:
@KorMay 10.2011 — Why so intricate? Wouldn't be easier something like this:
<i>
</i>function loadXML(method, url){
//...
if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) {
return xmlhttp.responseText);
}
//...
}

function initialize() {
var location= loadXML("GET", "http://example.com/testing.php");
// do whichever with location
}
Copy linkTweet thisAlerts:
@LeanderauthorMay 10.2011 — Because of this function:

[CODE]
[U]xmlhttp.onreadystatechange = function()[/U]
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
onsuccess(xmlhttp.responseText);
}

if(xmlhttp.readyState == 4 && xmlhttp.status == 0)
{
onsuccess(xmlhttp.responseText);
}
}
[/CODE]


If i do a return, it would never come out the xmlhttp.onreadystatechange? That's why i'm using a callback function.
Copy linkTweet thisAlerts:
@KorMay 10.2011 — status ==0? that means you are ready to work locally, if it is the case? If so, have you tried that?:
<i>
</i>xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 &amp;&amp; (xmlhttp.status == 200||xmlhttp.status == 0)) {
onsuccess(xmlhttp.responseText);
}

<i> </i> }
Copy linkTweet thisAlerts:
@LeanderauthorMay 10.2011 — Yes, i have tried that!

The only problem is that i'm not working locally. I get that status code on my Android phone / iPhone 4.0.

And if i do that. The xmlhttp.responseText is empty on my Android / iPhone 4.0

Do you know what's wrong with it? I have searched already the whole web.
Copy linkTweet thisAlerts:
@KorMay 10.2011 — Is it the [B]url[/B] written correct? If you write it as an absolute URL (like http://www.etc) it won't work. Is it a relative path there?
Copy linkTweet thisAlerts:
@LeanderauthorMay 10.2011 — Thx, the problem has been solved!
Copy linkTweet thisAlerts:
@KorMay 10.2011 — Thx, the problem has been solved![/QUOTE]
And the reason was? Please share the solution with us. To make us avoid a possible error, for the future ?
Copy linkTweet thisAlerts:
@LeanderauthorMay 10.2011 — Is it the [B]url[/B] written correct? If you write it as an absolute URL (like http://www.etc) it won't work. Is it a relative path there?[/QUOTE]

That was the solution ? Thx!
Copy linkTweet thisAlerts:
@KorMay 10.2011 — OK. The reason is simple: servers do often presume that a request to an absolute URL is a request to [I]another domain[/I], which is impossible without using a third, server-side, code (a proxy). the response [B]status=0[/B] usually cover that case.
Copy linkTweet thisAlerts:
@saykul_90Jan 16.2013 — would u plz specify wat relative link means?

i have specified the url as http://ip address:portno/the web service name

the web service is located on anoder machine so i have to specify the ip address
×

Success!

Help @Leander spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...