miracle_potential Posted July 18, 2008 Share Posted July 18, 2008 Ok I did some research on tizag on Ajax and learnt a few things on Javascript and thanks to you guys I have the best part of an Ajax script but I cant seem to get it to work I constantly get the error "Object expected" Below is the Ajax code I'm trying to use. function GetAjax() { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { } try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { } if (typeof XMLHttpRequest != 'undefined') { return new XMLHttpRequest(); } throw new Exception('No AJAX support'); } function new_cont(obj,text) { //obj is the same thing as an object returned by something like document.getElementById() try { var aobj = new GetAjax(); aobj.open("POST", "new_prod_constructor.php"+text, true); var p = "s=" + encodeURI(obj.innerHTML); //the post params. encodeURI is known to have some problems, but it'll be fine for this ;p //set some headers just in case aobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); aobj.setRequestHeader("Content-length", p.length); aobj.onreadystatechange = HandleSendResponse; aobj.send(p); } catch (e) { alert('error code1'); //worst error handling ever. EVER! } } function HandleSendResponse() { if(ajaxRequest.readyState == 4){ document.getElementById("pcatchd").innerHTML = this.responseText; } } And this is an example of the link I have in the div not entirely sure I fully understand all of this. <a href="javascript:new_cont(''paction=catagory_catalogue&catagory=catagory')">Catagory Here</a> If it isnt that clear I need the link to send the "text" to the function so when the innerhtml changes it loads differant URL variables for me to use in my PHP class Thanks guys all your help is greatly appreciated :] Quote Link to comment Share on other sites More sharing options...
xtopolis Posted July 19, 2008 Share Posted July 19, 2008 If that is an exact example of the link you're using to call it, it looks like you're missing an argument: function new_cont(obj,text) <--{2 args} <a href="javascript:new_cont(''paction=catagory_catalogue&catagory=catagory')"> <-- {1 arg} Also, it has an extra ' at the beginning.. and you spelt category wrong , thrice change your link to send two args, (you're missing the obj part) <a href="javascript:new_cont('idOfYourDiv','paction=catagory_catalogue&catagory=catagory')"> Quote Link to comment Share on other sites More sharing options...
corbin Posted July 19, 2008 Share Posted July 19, 2008 Hehe when ever I first opened this thread I was like, "That looks oddly familiar...." Then I was like, "Oh yeah!!!!" Anyway, a couple things: 1. aobj.open("POST", "new_prod_constructor.php"+text, true); In your links, text doesn't have a ?, so you're basically sending new_prod_constructor.phppaction=catagory_catalogue&catagory=catagory. 2. var p = "s=" + encodeURI(obj.innerHTML); Since obj isn't processed at all when you pass it as a parameter, you're going to have to do this every link: new_cont(document.getElementById('elementName'), 'text'); Instead, in the function, you could do something like: function new_cont(obj, text) { if(typeof obj != 'object') obj = document.getElementById(obj); That way, you could pass in an object or a text string. 3. if(ajaxRequest.readyState == 4){ Where is ajaxRequest set? First, the interpreter will look in the function for a variable by the name of ajaxRequest, and then, when it doesn't find it, the interpreter will look in the global scope. You could be trying to make it look in the global scope, but I would avoid that for two reasons. Globals in general are bad (google it), and I would try to keep the stuff contained in the object as much as possible, as to retail re-usability (with less confliction issues). Since the function is run from inside of the class, it is essentially a method inside of the class. Because of this, you can, and should, use this inside of the function (like done later with this.responseText). So, basically this.readyState. 4. Catagory is spelled category. x.x Quote Link to comment Share on other sites More sharing options...
xtopolis Posted July 19, 2008 Share Posted July 19, 2008 FYI corbin, I am 99.9% sure POST calls do not need a ? prepended. My other post Quote Link to comment Share on other sites More sharing options...
corbin Posted July 19, 2008 Share Posted July 19, 2008 Ahh, but he's passing GET variables as well. The essential prototype of a POST request is (raw HTTP headers): POST {url} HTTP/1.1 Host: somesite.com {post vars} Assuming aobj is an AJAX object, the following is true: The place holders above would go like this: aobj.open("POST", {url}, true); aobj.send({post vars}); Therefore, if {url} has stuff passed along with it, it does need a ?. POST new_prod_constructor.phppaction=catagory_catalogue&catagory=catagory HTTP/1.1 Would request the file new_prod_constructor.phppaction=catagory_catalogue&catagory=catagory, which would most likely not exist. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted July 19, 2008 Share Posted July 19, 2008 Ah! My mistake, missed that (longurl)+text statement. Confusing lol. For some reason I'm thinking that he should not be doing both? Possibly make whatever script he's calling be placed in it's own PHP file and just POST vars, or only GET vars? If anything, maybe this is out of my scope and I'm not understanding. Glad you understand lol. Quote Link to comment Share on other sites More sharing options...
corbin Posted July 19, 2008 Share Posted July 19, 2008 The strange part about the HTTP protocol is that GET can be mixed into a POST request, but as far as a I know POST can't be mixed into a GET request. Valid example: POST somepage.php?getvar=val HTTP/1.1 Host: somesite.com postvar1=val&postvar2=val2 Invalid (as far as I know -- never tried it to be honest, but I would expect it to be invalid) example: GET somepage.php?getvar=val HTTP/1.1 Host: somesite.com postvar1=val&postvar2=val2 An example of the first valid one would be to have a form submitting data via POST to a URL with a GET string. <form action="?p=register" method="POST"> Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted July 21, 2008 Author Share Posted July 21, 2008 I'm playing with it right now with my slight grasp on hehe. :] I understand that this is a POST function but what I don't understand is how I get the GET variables for my PHP to catch because I have a switch to catch those evil ones that play with the URL and to keep my code tidy. I've changed the function call that was for a non existent function but it still says object missing and line one character one so I'm pretty stumped out I'm still toying with it though :] thanks for your advice though guys :] Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted July 22, 2008 Author Share Posted July 22, 2008 Ok I worked out the "Object required" error but now haha theres no debugging information on this problem at all I'm in the process of finding a good Ajax IDE but just for now so I can actually learn something TT the links when pressed do absolutely sod all haha Basically the solution was instead of having the div definition outside of the function call on the link I put it as the first passing variable in the brackets (simple I know) And the links are now <a href="javascript:new_cont(pcatchd, '?paction=category_catalogue&category=test_category')">test link</a> But its just not doing anything at all... and all I need it to do is give that link to the function as "text" and it reload the url with the variables assigned in the link as innerHTML. function GetAjax() { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { } try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { } if (typeof XMLHttpRequest != 'undefined') { return new XMLHttpRequest(); } throw new Exception('No AJAX support'); } function new_cont(obj,text) { try { var aobj = new GetAjax(); aobj.open("GET", "new_prod_constructor.php" +text, true); var p = "s=" + encodeURI(obj.innerHTML); aobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); aobj.setRequestHeader("Content-length", p.length); aobj.onreadystatechange = HandleSendResponse; aobj.send(null); } catch (e) { window.location('http://new.noirorchidemporium.co.uk/errors/help/?error=ajx'); } } function HandleSendResponse() { if(GetAjax.readyState == 4){ document.getElementById("pcatchd").innerHTML = this.responseText; } } And I spelt category right this time Quote Link to comment Share on other sites More sharing options...
xtopolis Posted July 23, 2008 Share Posted July 23, 2008 your link: <a href="javascript:new_cont(pcatchd, '?paction=category_catalogue&category=test_category')">test link</a> may be the trouble... the first argument pcatchd ? shouldn't that be in quotes? 'pcatchd', is it a reserved work like event that I just don't know? try that, otherwise, maybe post more code. your code seems to have a good structure, but i seem to be missing exactly what you're wanting to happen. so post your main page where you have the divs in place for this info etc. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 23, 2008 Share Posted July 23, 2008 Why are you using this line of code: var aobj = new GetAjax(); GetAjax already returns an instantiated object, so why are you issuing new before GetAjax(). It should just be: var aobj = GetAjax(); Quote Link to comment Share on other sites More sharing options...
corbin Posted July 23, 2008 Share Posted July 23, 2008 Hrmmm, you're right. No idea why I was doing that. I think back in the day, when I first started using that function, it was just like return ActiveXObject("Msxml2.XMLHTTP");.... I remember having to use new before. Anyways, thanks for pointing that out ;p. Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted July 23, 2008 Author Share Posted July 23, 2008 Well I can post my PHP script as well if need be but I'm sure that wont help anyone. I put commers around the pcatchd (ID of my div) and it still does nothing. I've got myself an Ajax book which I'll read when I get home but for now I'd quite like to see a real person fix or at least clue I love a challenge but this is totally confusing haha. The code hasnt changed except I've taken out the new klaus. The page is jsut a debugging page witha div in it where I have a PHP class that pulls URL data and works from there. http://new.noirorchidemporium.co.uk/testhelp.php?paction=catagories Quote Link to comment Share on other sites More sharing options...
corbin Posted July 26, 2008 Share Posted July 26, 2008 Right now the code that you posted gives a 404 error on the AJAX request. Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted July 27, 2008 Author Share Posted July 27, 2008 I'm getting there, having one last issue I keep getting object doesnt support this action and I cant work it out I've used alerts to show me all thats going on and cant find it. <script type="text/javascript" language="javascript"> function GetAjax() { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { } try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { } if (typeof XMLHttpRequest != 'undefined') { return new XMLHttpRequest(); } throw new alert('No AJAX support'); } function new_cont(url) { try { var aobj = GetAjax(); aobj.open('GET', url, false); aobj.send(null); aobj.onreadystatechange = HandleSendResponse(aobj); } catch (e) { window.location('http://new.noirorchidemporium.co.uk/errors/help/?error=2'); } } function HandleSendResponse() { var naobj = GetAjax(); if(naobj.readyState==4){ document.getElementById("pcatchd").innerHTML = naobj.responseText; } } </script> Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted July 27, 2008 Author Share Posted July 27, 2008 Ah I just commented out the window.location() and it worked. How could I get it to forward to the error reporting system I've programmed if there is an error? Quote Link to comment Share on other sites More sharing options...
corbin Posted July 28, 2008 Share Posted July 28, 2008 Well that would depend on how your error reporting system works ;p. Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted July 28, 2008 Author Share Posted July 28, 2008 Its not so much that lol I just need it to do like a window.location thing but it doesnt like that at all Quote Link to comment Share on other sites More sharing options...
corbin Posted July 28, 2008 Share Posted July 28, 2008 What's wrong with a generic: window.location = "blah"; ? Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted July 31, 2008 Author Share Posted July 31, 2008 I cant remember the specific error it gave me now but it really protested against it. Also I've hit another problem with my Ajax, its all working and stuff but I have one extra problem thats probably somethign so stupid I'm missing it completely My Ajax script pulls open a page which in turn has links on it, and the links are supposed to perform the same action as the initial function so basically page opens(with onload feature) and links are shown->link clicked->ajax functions reused to load new content But it says that loadsite is undefined, which is wierd because the function was used to pull up the links ??? my code is // Get the HTTP Object function getHTTPObject(){ if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); else if (window.XMLHttpRequest) return new XMLHttpRequest(); else if (!window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); else if (!window.XMLHttpRequest) return new XMLHttpRequest(); else { alert("Your browser does not support AJAX. Please download Mozilla Firefox"); return false; } } // Change the value of the outputText field function setOutput(){ if(httpObject.readyState == 4){ document.getElementById('productspage').innerHTML = httpObject.responseText; } } // Implement business logic function loadSite(url){ httpObject = getHTTPObject(); if (httpObject != null) { httpObject.open("GET", url, true); httpObject.send(null); httpObject.onreadystatechange = setOutput; } else { getHTTPObject(); } } var httpObject = null; And the link is <a href="javascript:loadsite('http://new.noirorchidemporium.co.uk/includes/new_prod_constructor.php?paction=catagory_catalogue&catagory=test');">test</a> I've tried adding new to it and all sorts but I dont understand because i have a body onload using that function <body id="productspage" onload="loadSite('http://new.noirorchidemporium.co.uk/testhelp.php?paction=catagories');"> Quote Link to comment Share on other sites More sharing options...
corbin Posted August 1, 2008 Share Posted August 1, 2008 "<a href="javascript:loadsite" Functions are case sensitive in JS if I remember correctly. <a href="javascript:loadSite Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted August 1, 2008 Author Share Posted August 1, 2008 :o How the hell did I miss that Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.