/    Sign up×
Community /Pin to ProfileBookmark

AJAX Problem – Ready State 4 but no data

Hi,

I am having a problem with an AJAX request.

The URL in a browser works fine – that is, it returns data.

But when I use the same URL in an AJAX request, the data is not returned, but the ready state is 4.

Any ideas? I thought it might have something to do with a “chunked” response.

See code below.

[code=html]

<html>

<head>

<script type=”text/javascript” charset=”utf-8″>

function sendAusPostRequest() {
var urlString = getURLString();
loadXMLDoc(urlString, ausPostResponseReceived);
}

function getURLString() {
var urlString = “http://drc.edeliver.com.au/ratecalc.asp?Pickup_Postcode=6025&Destination_Postcode=6055&Country=AU&Weight=100&Service_Type=STANDARD&Length=100&Width=100&Height=100&Quantity=1”;
return urlString;
}

function ausPostResponseReceived(responseText) {
window.alert(responseText);
}

function loadXMLDoc(urlString, callbackFunction) {
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) {
callbackFunction(xmlhttp.responseText);
}
}
xmlhttp.open(“GET”, urlString, true);
xmlhttp.send();
}

</script>

</head>

<body>

<div onclick=”sendAusPostRequest()”>
Click On Me To Send Request
</div>

</body>

</html>

[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@taibaauthorJun 07.2010 — Apologies - this seems to work in IE, but not in FireFox.
Copy linkTweet thisAlerts:
@KorJun 07.2010 — You need also to check the [B]status[/B] property of the request/document:
<i>
</i>xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4){
[COLOR="Blue"]if(xmlhttp.status==200){[/COLOR]
callbackFunction(xmlhttp.responseText);
[COLOR="Blue"]}[/COLOR]
}

Copy linkTweet thisAlerts:
@taibaauthorJun 07.2010 — Actually - I think I am being a twit.

I didn't realise you cannot AJAX across domains.

I am trying to access http://drc.edeliver.com.au/ratecalc.asp from a different domain. That is, a static HTML file.

IE lets me do it. Firefox doesn't.

I think that's the problem... I'm just surprised I don't get a proper error message like I would if I used an iframe.
Copy linkTweet thisAlerts:
@KorJun 07.2010 — 
I didn't realise you cannot AJAX across domains.[/QUOTE]

use a combined server-side proxy + AJAX.
×

Success!

Help @taiba 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...