skip to Main Content

i am trying to get data via ajax. everything works fine except the "error handler". whenever i have error in my php i want that the error is sent to the html page (javascript file). it’s not working.

im using the success in my ajax.

What i want is to get alerted with the msg and error ive set in the PHP file (line 5 and 6)

Here is my code:

PHP

$result = mysqli_query($conn, $sql);

if (!$result || mysqli_num_rows($result) == 0) {
$resp = array(
    'status' => "Error",
    'msg'    => "Error Msg",
    'error'  => mysqli_error($conn)
);
echo json_encode($resp);
die();
} else {

while ($row = mysqli_fetch_array($result)) {
    $id = $row['id'];
    // $ownId = $row['own_id'];
    $name = $row['name'];
    $geraet = $row['manuf'].' '.$row['model'];
    $fail = $row['fail'];
    $abg_dat = $row['pick_date'];
    $status = $row['status'];

    $return_arr[] = array("id" => $id,
                    "name" => $name,
                    "geraet" => $geraet,
                    "fail" => $fail,
                    "abg_dat" => $abg_dat,
                    "status" => $status
                );
}
$resp = array(
    'status' => "Succ" ,
    'msg'    => "Daten erfolgreich abgerufen",
    'resp'   => $return_arr
);

echo json_encode($resp);
}

JS

$.ajax({
    url: `${phpDir}/inc/techniker.inc.php`,
    type: 'get',
    dataType: 'JSON',
    success: function(response){
        if (response['status'] == "Error") {
            alert(response['status'] +  "<br/>" + response['error']);
            
        } else {
            var len = response['resp'].length;
            var rspKey = response['resp'];
            for(var i=0; i<len; i++){
                var id = rspKey[i].id;
                var name = rspKey[i].name;
                var geraet = rspKey[i].geraet;
                var stoerung = rspKey[i].fail;
                var abgabe = rspKey[i].abg_dat;
                var status = rspKey[i].status;
                // example on muliple rows
                // var username = response[i].username;
                
                var tr_str = `
                <tr id="${id}" onclick="tabCaller(${id})">
                    <td>#${id}</td>
                    <td>${name}</td>
                    <td>${geraet}</td>
                    <td>${stoerung}</td>
                    <td>${abgabe}</td>
                    <td>${status}</td>
                </tr>
                `;
                $("#techniker_tbody").append(tr_str);
            }
        }
    }
});

UPDATE
when mysqli error is happening the php is breaking and showing the error before he sends the response. that why im not gettin the ‘error’ either the ‘msg’

2

Answers


  1. var arr = JSON.parse(response);
    if(arr["status"] == "Error"){
    alert(arr["error"]);
    }
    
    Login or Signup to reply.
  2. What @aryan said was right. You can refer to the following which explains it well. How to trigger jquery.ajax() error callback based on server response, not HTTP 500?

    When your "webservice" is returning a HTTP Statuscode 400, 401, 403, 405 or 5XX jQuery ajax event error will be triggered.

    $.ajax({
        url: `${phpDir}/inc/techniker.inc.php`,
        type: 'get',
        dataType: 'JSON',
        success: function(response){
            ...
        },
        error: function(error) {
           // alert, append to body
        }
    });
    

    Just make sure to send a http header with the response. The mysql error is resulting in a HTTP 500 (Internal Server Error).

    Like so:

    header_status(500);
    ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search