For one, use jQuery to help troubleshoot. It's going to make your life so much easier. Even if you end up wanting to use raw xmlhttprequest, I'd suggest bringing jQuery in to rule out xmlhttprequest problems in your code, and more quickly hone in on the real issue.
Translation: I'm not comfortable with raw xmlhttprequest, so in order to help you let's switch to jQuery. You can go back when the issue is resolved! =)
this.getCars = function(id) {
$.get("/getCars.php?q="+id, function(data) {
alert("response from server: " + data);
});
}
http://api.jquery.com/jQuery.get/
Also make sure you are using Chrome Dev Tools or Firebug to inspect the response from your server, it's possible that it's failing there.
Update:
Make sure your HTML page (that is making the ajax call) and the PHP script are running on the same domain (localhost). I noticed you were specifying the full http://localhost
URL in your ajax call. Ajax does not work cross-domain (though there are workarounds, look at JSONP if you really need to do this cross-domain). Best bet is to get your HTML page loading from the same domain as the PHP script.
Update 2:
Actual issue was that the OP was loading the HTML from a folder on his computer (not via http://localhost
) and trying to make an ajax call to http://localhost
. The ajax call was failing since this is technically cross domain.