Jump to content

What's wrong with my ajax code?


rain13

Recommended Posts

HelloCould anyone tell me what's wrong with my code? Why does it say: "Error during AJAX call. Please try again"?

<!DOCTYPE html><html><body><script>function getXMLObject()  //XML OBJECT{   var xmlHttp = false;   try {     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers   }   catch (e) {     try {       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+     }     catch (e2) {       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false     }   }   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers   }   return xmlHttp;  // Mandatory Statement returning the ajax object created}var xmlhttp = new getXMLObject();    //xmlhttp holds the ajax objectfunction ajaxFunction() {  var getdate = new Date();  //Used to prevent caching during ajax call  if(xmlhttp) {    xmlhttp.open("GET","https://www.google.ee",true); //calling testing.php using POST method    xmlhttp.onreadystatechange = handleServerResponse;    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	xmlhttp.send();   }}function handleServerResponse() {   if (xmlhttp.readyState == 4) {     if(xmlhttp.status == 200) {        document.getElementById("content").innerHTML=xmlhttp.responseText; //Update the HTML Form element     }     else {        alert("Error during AJAX call. Please try again");     }   }}ajaxFunction();</script> </body></html>
Link to comment
Share on other sites

I also tried jquery that I got from stackoverflow but still not working.

<!DOCTYPE html><html><head><script type='text/javascript' src='jquery.js'></script></head><body><script>var response = $.ajax({ type: "GET",                           url: "http://w3schools.invisionzone.com/index.php?showtopic=49551",                           async: false                      }).responseText;alert(response);</script> </body></html>
Link to comment
Share on other sites

It shows that message if the status is not 200. You should use your browser's developer tools to look at the request and see what the response code from the server is. Maybe it's 404. Maybe it's 500, meaning that there was an error on the server. Use your developer tools to verify.

Link to comment
Share on other sites

There's no client.side solution. For security reasons Javascript cannot access content that is on another domain. To get around it, people use what's called a proxy: a script on your server that gets content from another website. Here's one article about it: http://developer.yahoo.com/javascript/howto-proxy.html

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...