0

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'

xDw.Co
  • 41
  • 5

2 Answers2

0
var arr = JSON.parse(response);
if(arr["status"] == "Error"){
alert(arr["error"]);
}
aryanknp
  • 1,135
  • 2
  • 8
  • 21
0

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);
...
bro
  • 61
  • 6
  • refferring to the php code i wrote above. what should i write inside the error: ... ? – xDw.Co Jul 28 '20 at 15:26
  • also the status im getting is 200. not sure why. the error is that there are no such a table. the error is written for a test purpose. – xDw.Co Jul 28 '20 at 15:30
  • Since the mysql error will be parsed in the body. You can use error: function(error) { alert(error['responseText']); // here is the body } ... – bro Jul 28 '20 at 15:34
  • Can you try console outputting error with console.log(error). What is the output? – bro Jul 28 '20 at 21:24