rain13 Posted February 12, 2014 Share Posted February 12, 2014 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 More sharing options...
rain13 Posted February 12, 2014 Author Share Posted February 12, 2014 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 More sharing options...
justsomeguy Posted February 12, 2014 Share Posted February 12, 2014 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 More sharing options...
Ingolme Posted February 12, 2014 Share Posted February 12, 2014 I believe there's cross-domain protection stopping you from accessing data from other websites. Link to comment Share on other sites More sharing options...
davej Posted February 12, 2014 Share Posted February 12, 2014 Try... }else{ alert("Error during AJAX call: " + xmlhttp.readyState +":"+ xmlhttp.status); } Link to comment Share on other sites More sharing options...
rain13 Posted February 12, 2014 Author Share Posted February 12, 2014 Try... }else{ alert("Error during AJAX call: " + xmlhttp.readyState +":"+ xmlhttp.status); } Error during AJAX call: 4:0 Link to comment Share on other sites More sharing options...
rain13 Posted February 12, 2014 Author Share Posted February 12, 2014 I believe there's cross-domain protection stopping you from accessing data from other websites. What can I do about it? what http header do I have to change and to what? Link to comment Share on other sites More sharing options...
Ingolme Posted February 12, 2014 Share Posted February 12, 2014 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 More sharing options...
rain13 Posted February 12, 2014 Author Share Posted February 12, 2014 thanks Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now