Created
January 3, 2012 14:27
-
-
Save harit-sunrun/1555087 to your computer and use it in GitHub Desktop.
ajax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ajaxSuggest = false | |
function submitSuggestion() { | |
url = '/suggest/' | |
ajaxSuggest = false; | |
if (window.XMLHttpRequest) { // if we're on Gecko (Firefox etc.), KHTML/WebKit (Safari/Konqueror) and IE7 | |
ajaxSuggest = new XMLHttpRequest(); // create our new Ajax object | |
if (ajaxSuggest.overrideMimeType) { // older Mozilla-based browsers need some extra help | |
ajaxSuggest.overrideMimeType('text/xml'); | |
} | |
} | |
else if (window.ActiveXObject) { // and now for IE6 | |
try {// IE6 has two methods of calling the object, typical! | |
ajaxSuggest = new ActiveXObject("Msxml2.XMLHTTP"); | |
// create the ActiveX control | |
} catch (e) { // catch the error if creation fails | |
try { // try something else | |
ajaxSuggest = new ActiveXObject("Microsoft.XMLHTTP"); | |
// create the ActiveX control (using older XML library) | |
} catch (e) {} // catch the error if creation fails | |
} | |
if (!ajaxSuggest) { // if the object doesn't work | |
// for some reason it hasn't worked, so show an error | |
alert('Sorry, your browser seems to not support this functionality.'); | |
return false; // exit out of this function | |
} | |
ajaxSuggest.onreadystatechange = suggestionResponse; // when the ready state changes, run this function | |
// DO NOT ADD THE () AT THE END, NO PARAMETERS ALLOWED! | |
ajaxSuggest.open('POST', url, true); // open the query to the server | |
ajaxSuggest.send(null); // close the query | |
// and now we wait until the readystate changes, at which point | |
// suggestionResponse(); is executed | |
return true; | |
} | |
} | |
function suggestionResponse() { | |
// N.B. - in making your own functions like this, please note | |
// that you cannot have ANY PARAMETERS for this type of function!! | |
if (ajaxSuggest.readyState == 4) { // if ready state is 4 (the page is finished loading) | |
if (ajaxSuggest.status == 200) { // if the status code is 200 (everything's OK) | |
// here is where we will do the processing | |
if (ajaxSuggest.responseText == null) { // if the result is 1 | |
alert('Error in saving suggestion!'); | |
} | |
else { // otherwise | |
// data = ajaxSuggest.responseText.split(':') | |
// alert(String(data[0])); | |
// alert(ajaxSuggest.responseText); | |
alert('hey!') | |
window.location='/page/'+ str(ajaxSuggest.responseText); | |
} | |
} // end if | |
else { // if the status code is anything else (bad news) | |
alert('There was an error. HTTP error code ' + ajaxSuggest.status.toString() + '.'); | |
return; // exit | |
} | |
} // end if | |
// if the ready state isn't 4, we don't do anything, just | |
// wait until it is... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment