1

So I'm making a webapplication where you can play sudoku but the back-end is in Java. I made a servlet to communicate with my Jquery trough ajax.

I'm sending my generated array (the sudoku) to my webapp with this servlet trough Ajax. But when I do a typeof it seems to be undefined. Which means I cannot do any operations with the array

This is my Ajax call:

    $.ajax({
    url:"/2017_S2_Group_18/API/*",
    dataType:"json",
    success: function(data) {
        for(var i = 0; i<81;i++){
            originalSudoku[i] = $(data).attr("sudokuArray")[i];
            changedSudoku[i] = $(data).attr("sudokuArray")[i];
        }           
    }
});

orginalSudoku is the array that cannot be modified and changedSudoku is the array that is going to be modified.

This is the output I get by browsing to the URL from my Ajax call.

{"sudokuArray":[7,6,8,1,9,2,3,4,5,1,9,2,4,3,5,6,7,8,4,3,5,7,6,8,9,1,2,8,7,9,2,1,3,4,5,6,2,1,3,5,4,6,7,8,9,5,4,6,8,7,9,1,2,3,9,8,1,3,2,4,5,6,7,3,2,4,6,5,7,8,9,1,6,5,7,9,8,1,2,3,4]}

How can I parse/change my type to either string or char or integer?

1
  • Which array are you trying to work with that seems undefined? originalSudoku, changedSudoku, or both? Also, what does the html for that look like? Cuz if 'data' is the json at the end of your question... I don't think $(data).attr() is what you want. Commented May 16, 2017 at 16:13

1 Answer 1

2

The issue is because data is an object, and you're trying to use jQuery's attr() method, intended for DOM elements, to access a property of it. That's not going to work.

Instead, you can access the properties of data as you would any normal object in Javascript. Try this:

success: function(data) {
  for (var i = 0; i < data.sudokuArray.length; i++) {
    originalSudoku[i] = data.sudokuArray[i];
    changedSudoku[i] = data.sudokuArray[i];
  }           
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I've never worked with JSON before only with XML.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.