3

By some reason there's a parsing error with the ajax code below. How could I find out what it is, and/or can someone see what's wrong?

$('#listElements').sortable({
        //revert: true,
        update: function(event, ui) {

            var order = [];
            $('.listObject li').each(function (e) {
                order.push($(this).attr('id'));
            });
            $.ajax({
                type: "POST",
                url: "index.php?",
                dataType: "json",
                data: { json: order },                  error: function(jqXHR, exception) {
                    if (jqXHR.status === 0) {
                        alert('Not connect.\n Verify Network.');
                    } else if (jqXHR.status == 404) {
                        alert('Requested page not found. [404]');
                    } else if (jqXHR.status == 500) {
                        alert('Internal Server Error [500].');
                    } else if (exception === 'parsererror') {
                        alert('Requested JSON parse failed.');
                    } else if (exception === 'timeout') {
                        alert('Time out error.');
                    } else if (exception === 'abort') {
                        alert('Ajax request aborted.');
                    } else {
                        alert('Uncaught Error.\n' + jqXHR.responseText);
                    }
                }
            });
        }
holyredbeard
  • 19,619
  • 32
  • 105
  • 171
  • 1
    parseing error in this js code, or in the resulting json text on the server? – Marc B Oct 23 '12 at 14:59
  • have you tried removing the leading / on index.php? –  Oct 23 '12 at 15:00
  • 4
    Don't tell us what the exact problem is. It would take away all the fun of guessing! Everybody knows engineers *love* guessing. – Pekka Oct 23 '12 at 15:00
  • can you debug-output your `order` variable? – SkaveRat Oct 23 '12 at 15:00
  • Use the browsers debugging tools for JavaScript. It's often on F12 – Lukas Knuth Oct 23 '12 at 15:02
  • Sorry, I'm just not into AJAX so I don't know how the error messages work. Of course I've looked at the console, but there's actually no information there at all in this case. – holyredbeard Oct 23 '12 at 15:05
  • "no information"? so where do you get the "parsing error" from? – PiTheNumber Oct 23 '12 at 15:08
  • @PiTheNumber: From the if statement in the code which I first forgot to include. Sorry! – holyredbeard Oct 23 '12 at 15:11
  • 1
    ok, so the JSON from index.php is wrong. That means the code you posted has nothing to do with the problem. Can you post the JSON response or index.php? What browser do you use? – PiTheNumber Oct 23 '12 at 15:14
  • I use Chrome. How can I get the JSON response? – holyredbeard Oct 23 '12 at 16:06
  • Press F12 to open the console and select Network. There you see all file loads and AJAX requests. Select index.php and have a look at Response. Or you call index.php with the needed parameter manually. Did you write index.php yourself? I also added another way to my answer. – PiTheNumber Oct 24 '12 at 15:23

2 Answers2

1

There is no parsing error in this JavaScript code.

Please post the response of "index.php" and the error message you got.

Have a look at the response data. Open index.php in the browser, press F12 and insert this into the console:

       $.ajax({
            type: "POST",
            url: "index.php",
            //dataType: "json",
            data: { json: order },
            success: function(data) {
               console.log(data);
            }
        });
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
1

data: { json: order } ... it's not well formatted...

sataniccrow
  • 372
  • 2
  • 7