Jump to content

Cause of the error..


echofool

Recommended Posts

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

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

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

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

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

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.

post-41085-0-34230300-1329782591_thumb.jpg

Link to comment
Share on other sites

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...