jug Posted January 27, 2010 Share Posted January 27, 2010 Hi, I have been developing this script for a few days with its objective to make ajax requests scripts a lot easier to write each time. The errors Im getting are mainly server readystate and status errors (not being the correct code). Also I am fairly new to making custom JavaScript objects. If anyone could quickly look through the script and point anything out itll be much appreciated. Thanks in advance jug calling the function function process(){ var XHR = new ajaxRequest(scriptPath, 'XML', response); XHR.doPost(params); } return false; } function response(xmlResponse){ //stuff here } custom ajax object function ajaxRequest(url, responseType, callback){ var req = init(); req.onreadystatechange = processRequest; var errorText = 'no errors'; function init(){ if(window.XMLHttpRequest){ return new XMLHttpRequest(); }else{ errorText = 'Your browser does not support the XMLHttpRequest object.'; } } function processRequest(){ if(req.readyState != 4){ errorText = 'Server ready state is not 4.'; }else{ if(req.status != 200){ errorText = 'Server status is not 200.'; }else{ switch (responseType){ case 'Text': callback(req.responseText); break; case 'XML': callback(req.responseXML); break; default: errorText = 'Incorrect server response type.'; } } } } this.doPost = function(params){ if(req.readyState == 0 || req.readyState == 4){ try{ req.open("POST", url, true); req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); req.send(params); }catch(e){ errorText = 'XMLHttpRequest POST url not found.'; } }else{ setTimeout('doPost(params)', 1000); } } this.doGet = function(){ if(req.readyState == 0 || req.readyState == 4){ try{ req.open("GET", url, true); req.send(null); }catch(e){ errorText = 'XMLHttpRequest GET url not found.'; } }else{ setTimeout('doGet()', 1000); } } this.getErrorText = function(){ return errorText; } } Link to comment https://forums.phpfreaks.com/topic/190015-ajax-request-script-probs/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.