echofool Posted February 20, 2012 Share Posted February 20, 2012 Hey, I have a javascript issue, where by im getting an error but cannot work out why. Im compiling data and using an onFailure to load an error alert. But from what i can tell it should not error. This is my script: startLoading: function() {Loader.busy();Loader.unload();Loader.result.innerHTML = 'Loading data...';var myRequest = new Request.JSON({ url: '?load/user', onSuccess: function(obj){user.nick = obj.username;user.id = obj.id;user.setStartPos(obj.x*1,obj.y*1);user.map = obj.map_id*1;if (obj.aid)alarm.join(obj.aid);elseLoader.loadImages(); },onFailure: Loader.error('Data error!')}); myRequest.send();} According to my browser the data being called back is: {"aid":null} But i get the "Data error" alert =/ how on earth can i debug where the problem might be coming from? Link to comment Share on other sites More sharing options...
[dx] Posted February 20, 2012 Share Posted February 20, 2012 Firebug? Link to comment Share on other sites More sharing options...
thescientist Posted February 20, 2012 Share Posted February 20, 2012 I would try looking in the error console and see if you are getting any errors, otherwise look in the documentation for your Loader class and see if there's anything in there about it. Link to comment Share on other sites More sharing options...
echofool Posted February 20, 2012 Author Share Posted February 20, 2012 The odd thing is - the stuff still loads =/ so i dunno why its still erroring. What is it checking for onFailure? Does it have to be given a certain value for it to count as a failure =/? That then would help me know what i am looking for Link to comment Share on other sites More sharing options...
justsomeguy Posted February 20, 2012 Share Posted February 20, 2012 The error conditions would be in the Request.JSON class, which takes the success and failure handlers. Link to comment Share on other sites More sharing options...
ShadowMage Posted February 20, 2012 Share Posted February 20, 2012 What is it checking for onFailure? Does it have to be given a certain value for it to count as a failure =/?You haven't shown the entire class. There is no way to tell from what you've posted. As scientist has suggested, check the documentation for your Loader class. Link to comment Share on other sites More sharing options...
echofool Posted February 20, 2012 Author Share Posted February 20, 2012 Difficult to show all of it cos its over a few thousand lines long. Theres no documentation cos I have made the thing myself over many months ! The returned arrays are all syntax'd correctly. I'll have to keep searching for the cause, very hard to debug JS Link to comment Share on other sites More sharing options...
justsomeguy Posted February 20, 2012 Share Posted February 20, 2012 You made it yourself but you don't know what it's checking for in the failure conditions? The code you're looking for is in the Request.JSON class. That's what you send the success and failure functions to, and that is what decides which function to use as the callback. If all of the code in the success function runs, and you still see the error, then you probably need to replace this: onFailure: Loader.error('Data error!') with this: onFailure: function() {Loader.error('Data error!')} Link to comment Share on other sites More sharing options...
echofool Posted February 21, 2012 Author Share Posted February 21, 2012 I mistyped i'm using mootools but the loading script is longgg it loads alot of stuff - but the documentation has this: ( i use mootools) failure Fired when the request failed (error status code). Signature: onFailure(xhr) Thats it lol - epic helpful their documentation. The error status code im assuming is the the page status when you send the request - which is 200 according to firebug. See attatchment. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 21, 2012 Share Posted February 21, 2012 And is Loader.error actually causing an error message to appear, or does it return a function that displays an error message? See the end of my previous post. Link to comment Share on other sites More sharing options...
echofool Posted February 21, 2012 Author Share Posted February 21, 2012 It calls a function like this: onFailure: Loader.error('data error!') This is the only error with that "data error" message, so i know which onFailure it is. The function it calls is: var Loader = {//other js stuff error: function(a) {alert('Error! ' + a);} // other js stuff} Link to comment Share on other sites More sharing options...
justsomeguy Posted February 21, 2012 Share Posted February 21, 2012 Then that's the problem, you're executing that function immediately instead of using it as the onFailure handler. Link to comment Share on other sites More sharing options...
echofool Posted February 21, 2012 Author Share Posted February 21, 2012 Hmm thats strange, the original script had it worded like that but doesn't do what my localhost script is doing. V.odd. the only difference on the original script is the function call is like this: onFailure: Loader.error error: function() {alert('Error, refresh window.');} It didn't send a variable like my edited version did.. could that change the way it behaves? Seems unlikely though.. EDIT yup i was correct, if i remove the ('Data error') part it seems to solve the problem. Link to comment Share on other sites More sharing options...
thescientist Posted February 21, 2012 Share Posted February 21, 2012 A reference to a function and function call are two different things. Often times, as in the case with callbacks, you often see functions that need to take arguments returned within a function scope, i.e. property : function(){ return callbackFunction(arg){ console.log("inside callback function"); };} Link to comment Share on other sites More sharing options...
justsomeguy Posted February 21, 2012 Share Posted February 21, 2012 Like I showed in post 8, when you have the parentheses on the end of the name of a function then you are executing the function, not pointing to it. In order for that to work, the error function would have needed to return a function which would have been used as the failure handler. So you were executing the error function immediately at the point when you make the JSON object, not telling the JSON object to use that as the failure handler. You can wrap it in an anonymous function if you want to call a function with parameters. Mootools might also have a way to make a copy of a function that will run with certain parameters, and use that as the failure handler. It looks like that's what this does: http://mootools.net/docs/core/Types/Function#Function:pass onFailure: Loader.error.pass('error text') Using Loader.error.pass will return a copy of the Loader.error function that automatically will get passed whatever value you send. Link to comment Share on other sites More sharing options...
echofool Posted February 21, 2012 Author Share Posted February 21, 2012 Thanks for the information i will do some testing with it Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.