DataTables Ajax source error

DataTables Ajax source error

jpublicjpublic Posts: 10Questions: 5Answers: 0

I'm using jquery DataTables 1.10, the problem i'm having is the table loads the data and such from a service and the json is valid but after a few tries running it displays an alert: http://datatables.net/tn/1 periodically. So sometimes it runs perfectly fine then the alert just shows then i refresh and it loads the data. I research online but to no success, what am i doing wrong, thanks for your help in advance.

Answers

  • zepernickzepernick Posts: 32Questions: 7Answers: 0

    Something is happening on your server intermittently that is causing a invalid json response, such as, maybe it is sending an error page in html. Your best bet is to hit F12 in whatever browser you are in to bring up the devleoper tools. Find the network monitor tool which should allow you to see the ajax requests as they fire off. Try to reproduce the error and you should be able to see the actual response of the ajax request in the developer tool. That will probably give you a better idea of how the error is happening on the server.

  • jpublicjpublic Posts: 10Questions: 5Answers: 0
    edited November 2015

    i did what you suggested but the error still persist i would like to send a screenshot of issue what i'm getting, but it seems the forum doesn't allow it, what are my other options?
    Here is a link to the screenshot:
    https://www.dropbox.com/s/629cd5v03a3ahgo/Screenshot%202015-11-17%2012.52.30.png?dl=0

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    The screenshot suggests there was no response from the server (i.e. empty data). Which is not valid JSON. As to why it might not return any data you would need to debug the server-side script.

    Allan

  • jpublicjpublic Posts: 10Questions: 5Answers: 0
    edited November 2015

    Okay, but lets say 2 out of 10 times is comes back as valid json and loads the data, do you still believe it might be the sever-side script?

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    Without question if it comes back from the server any amount of times from the server as invalid JSON. Its transmitted over TCP, so there aren't going to be any missing packets - so short of a dropped connection the issue is with the server-side script.

    Allan

  • jpublicjpublic Posts: 10Questions: 5Answers: 0

    Can you take a look at the code, maybe you can identify what i did wrong

    ```
    <?php
    $inCucode = "GLLL01";
    $inProdCode = $_COOKIE['productSelected'];
    $inBuyNum = $_COOKIE['customerSelected'];

    try{
    $conn = db2_connect("*LOCAL", '', '');
    // Prepare Stored Procedure call //
    $proc = 'CALL WLKLIB.DSH3518SQ(?,?,?)';
    $stmt = db2_prepare($conn, $proc) or die("db2_prepare failed " . db2_stmt_error(). " and " .db2_stmt_errormsg());
    db2_bind_param($stmt, 1, 'inCucode', DB2_PARAM_IN,DB2_CHAR);
    db2_bind_param($stmt, 2, 'inProdCode', DB2_PARAM_IN,DB2_CHAR);
    db2_bind_param($stmt, 3, 'inBuyNum', DB2_PARAM_IN,DB2_CHAR);
    db2_execute($stmt);

    $result = array();
    while ($data = db2_fetch_assoc($stmt)) {
      $result[] = $data;           
    }
    
    $data = array("data" => $result);
    
    // JSON Response
    header('Content-type: application/json');    
    echo json_encode($data);
    

    } catch(Exception $e){
    header('HTTP', true, 500);
    }

    <?php > ``` ?>
  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Try taking the last ?> off, you shouldnt ever use that in server responses, php doesnt need it, and it sometimes just adds a return that breaks the json parsing

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    Unfortunately there is really no way to tell just from that code. As you say, it works most of the time, so there is obviously a timing issue somewhere. It could be that there is a database lock and timeout happening for example.

    I would suggest checking the server's error logs. Also try load testing the server.

    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    I received that error once, and it was because of the ?> :P Try removing it

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    It would be consistent if that was the issue though.

    I don't close PHP files myself either though - I remember the moment of revelation when someone told me they don't need that. Hurrah!

  • jpublicjpublic Posts: 10Questions: 5Answers: 0

    Still having the error guys although i removed the closing php tag...

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    I don't close PHP files myself either though - I remember the moment of revelation when someone told me they don't need that. Hurrah!

    Same! Its best practice to leave itbout wherever possible

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    Did you take a look at the server's error logs as I suggested above? I would also suggest enabling slow query logging on your SQL server if it isn't already and taking a look at that log.

    Allan

This discussion has been closed.