views:

34

answers:

3

When I create request to the server:

<script language="javascript" type="text/javascript">
function ajaxFunction()
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("Your browser broke!");
return false;
}
}

} ajaxRequest.onreadystatechange = function(){

if(ajaxRequest.readyState == 4){

document.write(ajaxRequest.responseText);

document.myForm.time.value = ajaxRequest.responseText;
    }
}
ajaxRequest.open("GET", "http://www.bbc.co.uk", true);
ajaxRequest.send(null); 

}

</script>

Why response is nothing? Why response isnt html code of this web site?

A: 

You might consider using a library like jQuery to do AJAX requests - they handle all the cross-browser quirks for you, and do a lot of extra work to boot. Your code could be as simple as

$.get(
  'http://www.bbc.co.uk',
  function(data) {
    // do something with data
  }
);`
ceejayoz
A: 

What browser are you trying this in? IE I presume from the code? In any event I guess you may well be hitting some sort of cross domain AJAX security issue.

Martin Smith
+1  A: 

This should work (I don't have time to test it right now.)

function ajaxFunction() { //Added open {
    var ajaxRequest;
    try{
    ajaxRequest = new XMLHttpRequest();
    } catch (e){
// Removed additional try / catch
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
        alert("Your browser broke!");
        return false;
        }
    } 
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            document.write(ajaxRequest.responseText);
            document.myForm.time.value = ajaxRequest.responseText;
        }
    }; // Added semi-colon to the end of the anonymous function definition
ajaxRequest.open("GET", "http://www.bbc.co.uk", true);
ajaxRequest.send(null); 
}

A few notes:

  • White space is not required for the most part in Javascript, but proper indentation makes it much easier to spot syntax errors.
  • When you bind an attribute to an anonymous function you need to follow your } with a ;
  • Once you understand how this works, dig into one of the larger libraries ajax functions / modules. (Learning is always a good thing, and ajax is one of those areas that really needs a few dozen man-hours of work to encounter all the differences between browsers.)+

ADDENDUM:

Cross-domain ajax requests are very difficult to do right (i.e. safely, securely, and without throwing errors) -- they are forbidden to javascript directly by the Same-Domain Origin policy.

See this question and this one for more discussion on the subject and ways to get around it with a proxy or with jsonp

+ jQuery's ajax function is 325 lines long (and that's not counting $.ajax.settings or $.extend())

Sean Vieira
it is the same, response came, but responseText is null
john
it is working when url is in the same domain for example url="something.html" but when it is cross site url it isnt working.
john
@john -- I updated my answer to briefly address cross-domain requests.
Sean Vieira
Good detailed answer :-)
Martin Smith