Confusing TypeError: Cannot read property 'n' of undefined
In an ajax call (for a game) that tries to pass two values in a dictionary, only one of the values se开发者_运维知识库ems to be getting read in, while the other (an instance of a JavaScript class) seems to be throwing "TypeError: Cannot read property 'col' of undefined." This is despite the fact this call is built identically to other calls in the same code that work fine.
Here's the class that seems to be causing the problem:
function Cell(col, row) {
this.col = col;
this.row = row;
// as well as an .eq and .ne and one or two other simple methods
}
Here's the setup for the call:
uCell = new Cell(-1, -1); // creates the Cell instance to pass
var dataout = {
ucell:uCell,
boardnumber:G.BOARDNUM,
};
// able to alert out both ucell and uCell values here just fine as well as boardnumber
And here's the call itself:
rqst_cellboard = $.ajax({
type: "POST",
data: dataout,
url: "/project03/cellboardcall/", // this never gets called
dataType:"json",
error: // etc.
success: // etc.
The error is thrown before the ajax call is made, apparently as it is being set up. Looking at the context
object at a jQuery breakpoint where the error is thrown, it looks like the data
value only has boardnumber
in it, not ucell
. The error reads TypeError: Cannot read property 'col' of undefined
. I can't figure out where in the jQuery to find what argument is undefined, but I'm assuming it's ucell
, since that's what should have the col
property.
Everything seems to be defined nicely going into the call, but the call itself is getting borked. I'm sure this is something pretty simple, but I can't figure it. Grateful for any ideas.
rqst_cellboard = $.ajax({
type: "POST",
data: dataout,
url: "/project03/cellboardcall/", // this never gets called
dataType:"json",
error: // etc.
success: // etc.
I bet the problem is related to the fact "dataout" is not a properly-formatted JSON object. In particular, it has a reference to uCell, which itself is not a JSON object, since you say it has methods implemented.
JSON objects have to follow a fairly rigid set of rules, in terms of what sorts of properties they can have. You will likely need to create a new object just for the ajax call, which only contains the properties you actually want to transfer.
Get rid of the trailing comma:
boardnumber:G.BOARDNUM, // <-- evil comma
IE is (and always has been) picky about stray commas in object and array literals.
精彩评论