1

i have a drop-down select tag use for showing the data from my database i also use ajax request to fetch my data from database but the problem i encounter is that when i get the result it how on my error:function()

admin_table.on('click', '#pay', function() {
    $tr = $(this).closest('tr');

    var data = admin_table.row($tr).data();
    $(document).on('click', '#dr_1', function(){
        $.ajax({
            type:'POST',
            url:'../functions/ajax/get_delivery_receipts.php',

            data:{
                id: data['user_id']
            },
            dataType:'JSON',
            sucess: function(data){
                console.log(data);
            },
            error: function(err){
                console.log(err.responseText);
            }
        });
    });
}); 

this is my function.. reason for that structure is the '#pay' is a button from the table.. and '#dr_1' is inside the modal that pop up when the 1st id is a click.

if (isset($_POST)) {
    $id = $_POST['id'];
    $res = $db->dr($id);
    echo json_encode($res);
} 

and this is inside the ajax request.

public function dr($id)
{
    $sql="SELECT * FROM tbl_delivery WHERE user_id = '$id'";
    var_dump($sql);
    $stmt = $this->dbh->prepare($sql);
    $stmt->execute();
    $data = array();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        $data[] = $row;
    }
    return $data;
}

and this is my query

Vaibhav Bhavsar
  • 432
  • 4
  • 12
Lion Smith
  • 647
  • 3
  • 16
  • 48
  • What is `admin_table.row`? – Musa Dec 01 '17 at 05:27
  • So what error do you get? An `error` in an AJAX call usually has to do with the HTTP request failing. PHP itself always returns a `200` status,. even if it encounters an error. So getting `400` or `500` means something is wrong with the request itself. – Twisty Dec 01 '17 at 05:28
  • check your url. – スージン Dec 01 '17 at 05:28
  • actually the error i get is the result that supposed to be inside success result – Lion Smith Dec 01 '17 at 05:28
  • check developer tools network tab - what http response code do you get – Jaromanda X Dec 01 '17 at 05:30
  • i didn't get any 400 or 500 error the console i get from error function is my result that supposed to be on my success – Lion Smith Dec 01 '17 at 05:30
  • 1
    JSON response is probably malformed - is the server yours? – Jaromanda X Dec 01 '17 at 05:31
  • try `http_response_code(200); header('Content-Type: application/json');` before `echo json_encode($res);` – Pejman Dec 01 '17 at 05:31
  • Agree with @JaromandaX, the `var_dump()` is going to cause major issues. – Twisty Dec 01 '17 at 05:32
  • @Twisty - didn't even see that in the PHP code, definitely the issue – Jaromanda X Dec 01 '17 at 05:32
  • 1
    @LionSmith also look here: https://stackoverflow.com/questions/4064444/returning-json-from-a-php-script – Twisty Dec 01 '17 at 05:33
  • it's on my local... i use `var_dump()` to check if my query returns a value. but yes it returns a value but the problem is it goes on my error function not on the success – Lion Smith Dec 01 '17 at 05:33
  • yes, and what does the response look like? is it VALID JSON? – Jaromanda X Dec 01 '17 at 05:34
  • [{"id":"1","user_id":"201720000001","delivery_receipt":"12342","delivery_date":"2017-11-30","due_date":"2017-11-30","amount_delivery":"50000","amount_paid":null,"amount_remaining":null,"transaction_status":"accepted","payment_status":"unpaid","created":"2017-11-29 06:55:34","modified":null}] that is the result i get on my error – Lion Smith Dec 01 '17 at 05:34
  • interesting that you don't get any output from the var_dump ... or are you simply ignoring that and pretending it's not there? – Jaromanda X Dec 01 '17 at 05:36
  • there is some error in your framework maybe, but what ever it is, it causes the response code to be anything but `200`, look at this https://en.wikipedia.org/wiki/List_of_HTTP_status_codes – Pejman Dec 01 '17 at 05:37
  • the output from the var_dump goes to the error and that is the json that is being returned.. – Lion Smith Dec 01 '17 at 05:38
  • 4
    `success` not `sucess` – Mohamed-Yousef Dec 01 '17 at 05:39
  • the var_dump would look like `string(46) "SELECT * FROM tbl_delivery WHERE user_id = '1'"` which isn't JSON – Jaromanda X Dec 01 '17 at 05:39
  • 1
    Wow, such a nice look, RESPECT @Mohamed-Yousef – Pejman Dec 01 '17 at 05:40
  • as much as the misspelling of success is why success isn't called, that would not cause the error handler to be called instead – Jaromanda X Dec 01 '17 at 05:41
  • 1
    didn't see that.. thank you so much.... @Mohamed-Yousef – Lion Smith Dec 01 '17 at 05:41
  • change error handler to `error: (x,s,e) => console.log(e)` - and I'm sure you'll get an error regarding a JSON parse error – Jaromanda X Dec 01 '17 at 05:46
  • simple solution - get rid of `var_dump` ... try your code ... see that error doesn't get triggered ... – Jaromanda X Dec 01 '17 at 05:48
  • if i remove the `var_dump()` nothing is being called even the error doesn't show up.. well @Mohamed-Yousef saw it through that i misspelled the `success` to `sucess`... and now it's good. the result goes to the success.. – Lion Smith Dec 01 '17 at 05:50
  • A way bigger issue with your code is that you're **wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php)** and should really use parameterized [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) . You're already using `prepare()` but you're not using parameterized prepared statements, which is what would protect you properly. – M. Eriksson Dec 01 '17 at 06:22
  • See. var dump was the reason error handler was called. Bad spelling was the reason success was not called – Jaromanda X Dec 01 '17 at 06:53

1 Answers1

1

error

Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )

Would advise:

    $(document).on('click', '#dr_1', function(){
      $.ajax({
        type: 'POST',
        url: '../functions/ajax/get_delivery_receipts.php',
        data: {
          id: data['user_id']
        },
        dataType: 'JSON',
        success: function(data, status, jqx){
          console.log(data, staus, jqx);
        },
        error: function(jqx, status, err){
          console.log(jqx, status, err);
        }
      });
    });

I suspect that sucess was the issue. It's success.

Community
  • 1
  • 1
Twisty
  • 30,304
  • 2
  • 26
  • 45
  • 1
    why would having no `success` callback cause the `error` callback to be called? you don't HAVE to have a `success` callback – Jaromanda X Dec 01 '17 at 05:40
  • @JaromandaX no idea. Have never tried running ajax with data being sent back, and no `success` callback. Maybe it catches that as the error and thus sends the successful HTTP Result to `error`. *shrug* – Twisty Dec 01 '17 at 05:42
  • 1
    nope, because $.ajax can be used without any handlers, as a pseudo jquery almost promise type thing - I can assure you having a `sucess` property and no `success` property does not trigger the `error` handler – Jaromanda X Dec 01 '17 at 05:47
  • @JaromandaX would agree, yet the returned `jqXHR` has the associated data, regardless of which callback is used. What's not clear to me is why was `error` callback triggered when status was *successful* (IE: 200). – Twisty Dec 01 '17 at 06:10
  • @JaromandaX a bit of research still points to what you suggested, that a malformed JSON result would trigger `error` callback. Not seeing any further explanation. – Twisty Dec 01 '17 at 06:13
  • https://stackoverflow.com/questions/6186770/ajax-request-returns-200-ok-but-an-error-event-is-fired-instead-of-success – Twisty Dec 01 '17 at 06:17