Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XMLHttpRequest unable to receive any response 1

Status
Not open for further replies.

princerozario

Technical User
Joined
Jan 19, 2007
Messages
1
Location
GB
Hello

I am trying to receive a simple response through the help of javascript and XMLHttpRequest. It is not giving me a result. Could you tell where I have done it wrong.

When I ran the page it gave me an error message

"There was a problem with the request"

Here is the code. Thankyou

<html>
<head><title>Test XMLHttpRequest.. to XML</title></head>
<body>
<script type="text/javascript" language="javascript">

function makeRequest(url)
{
var http_request=false;

try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {

try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){}
}

if (!http_request){
alert('Giving up :( Cannot create an XMLHTTP instance ');
return false;
}

http_request.onreadystatechange = function()
{
alertContents(http_request);
}
http_request.open('GET', url, true);
http_request.send(null);


function alertContents(http_request)

{
if (http_request.readyState == 4)
{
if (http_request.status == 200)
{
alert(http_request.responseText);
} else
{
alert('There was a problem with the request');
}

}
}

}



</script>

<span
style="cursor: pointer; text-decoration: underline"
onclick="makeRequest('C:\Documents and Settings\Owner\My Documents\test.html')">
Make a request
</span>
</body>
</html>
 
Looks like the problem lies here:
Code:
onclick="makeRequest('C:\Documents and Settings\Owner\My Documents\test.html')">
Not only is the backslash an escape character in JS - which would mean the correct format would be \\ instead of single slashes, I'm fairly sure that the http request actually requires a http url rather than a file on the local drive.

try:
Code:
onclick="makeRequest('[URL unfurl="true"]http://www.google.com')">[/URL]
and see how it goes.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
[1] If you are testing on local file system as well as using HTTP, apart from the escaping backslash,...
>onclick="makeRequest('C:\Documents and Settings\Owner\My Documents\test.html')">
[tt]onclick="makeRequest('C:\[red]\[/red]Documents and Settings\[red]\[/red]Owner\[red]\[/red]My Documents\[red]\[/red]test.html')">[/tt]

[2] ... you should allow .status be zero as well.
>if (http_request.status == 200)
[tt]if (http_request.status == 200 [blue]||http_request.status == 0[/blue])[/tt]
 
Top tip tsuji (part [2]), I've only been working with the http request object for four years or so and I never knew that.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
It takes two to dance. Glad you spot my little discovery useful. Thank, dt!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top