Jquery post.fail even if php succeeds

I have a problem with my post jQuery query:

$.post( 'http://localhost/***/ajax_bdd-change.php', {'id': _id, 'id_key': id_key, 'table': table, 'data': data}) .fail(function(jqXHR, textStatus, errorThrown){ alert('Erreur: '+jqXHR.responseText); }) .done(function(data){ alert($(data).text()); }); 

And my PHP:

 <?php $id = json_decode($_POST['id']); $id_key = json_decode($_POST['id_key']); $table = json_decode($_POST['table']); $data = json_decode($_POST['data']); foreach ($_POST as $k=>$v) { unset($_POST[$k]); } $rlt = array( 'erreur' => false, 'request' => 'none' ); $tmp = 0; $request = 'UPDATE '.$table.' SET'; foreach ($data as $target => $value) { if ($tmp++>0) $request = $request.','; $request = $request.' '.$target.' = "'.$value.'"'; } $request = $request.' WHERE '.$id_key.' LIKE "'.$id.'"'; $rlt['request'] = $request; require('BDD_connexion.php'); if (!$rlt_bdd = mysqli_query($link, $request)){ $rlt['erreur'] = 'Erreur: Update not done'; } $link->close(); echo json_encode($rlt); exit(); ?> 

Every time I run my code, it goes the same way:

  • PHP is executed correctly.
  • running jQuery .fail()
    • jqXHR.responseText is empty

I am trying to get php to crash and while jQuery is running the executed (function) correctly.

  • PHP has some error.
  • Running jQuery .done()
    • warning shows php error

I tried many things how to force UTF8 encoding for every php string variable. I am even trying to overlay a simple string like json_encode('hello world');

After many tests, it seems to me that my previous information:

It may be useful to explain that:

  • my javascript is inside the laod() php page.

Therefore, it should have such a structure as:

  • main.php --jQuery -> load (second.php in div )
    • second.php --jQuery → $.post (ajax_bdd-change.php)
    • ajax_bdd-change.php --return $rlt rlt → second.php (part of jQuery)

I do not mention this because I do not find it suitable.

It is the cause of this problem. I tried calling my php by mail from a new html page without .load , and it works fine.

+6
source share
4 answers

I found a resolution. Actually my code is more complicated, and jQuery .post is executed in .submit <form> inside the jQuery DialogBox script part. So the form was finished to be submitted before php response when it has a database request.

In resolution, I only change this submit with .click , and I close it manually when I get a response from the server.

0
source

The response code, if nothing bad happened on the server, should be 200 .

Most likely, based on your observations, the response code is something other than 200 . Also note that jQuery or any other infrastructure does not know whether native code written on the server was coherently executed. Typically, the only metric for the client is the response code .

jQuery source

+1
source

First you need to decide that you want JSON in your AJAX Query as follows:

  $.ajax({ url: 'http://localhost/***/ajax_bdd-change.php', dataType: 'json', //EXPLICITLY SET THIS TO JSON cache: false, type: "POST", data: { id : _id, id_key : id_key, table : table, data : data }, success: function (data, textStatus, jqXHR) { if(data){ alert("DATA CAME BACK AS JSON..."); console.log(data); // YOU REALLY DON'T WANT TO DO THIS: $(data).text() // THE .text() METHOD IS FOR THE DOM AND YOU ARE IN READING MODE TOO // data SHOULD CONTAIN THESE: request AND erreur // SO YOU CAN EITHER CHECK THE SQL OR THE ERROR LIKE SO: // I WOULD PREFER USING console.log(data.request) alert(data.request); alert(data.erreur); } }, error: function (jqXHR, textStatus, errorThrown) { alert('Erreur: '+jqXHR.responseText); }, complete: function (jqXHR, textStatus) { //DO SOMETHING HERE IF YOU WISH TO } }); 

And this is your PHP code:

  <?php $id = isset($_POST['id']) ? htmlspecialchars(trim($_POST['id'])) : null; $id_key = isset($_POST['id_key']) ? htmlspecialchars(trim($_POST['id_key'])) : null; $table = isset($_POST['table']) ? htmlspecialchars(trim($_POST['table'])) : null; $data = isset($_POST['data']) ? htmlspecialchars(trim($_POST['data'])) : null; foreach ($_POST as $k=>$v) { unset($_POST[$k]); } $rlt = array( 'erreur' => false, 'request' => 'none' ); $updateSQL = 'UPDATE '. $table . ' SET '; if($data){ foreach ($data as $target => $value) { $updateSQL .= ' ' . $target . ' = "' . $value . '", '; } $updateSQL = rtrim($updateSQL, ", "); // LIKE CLAUSE IS NOT SO FUNKY HERE: DO YOU WANT TO DO "EQUALS"? // ' WHERE ' . $id_key . ' = "' . $id. '"'; $condition = ' WHERE ' . $id_key . ' LIKE "' . $id. '"'; $request = $updateSQL . $condition; $rlt['request'] = $request; require('BDD_connexion.php'); if (!$rlt_bdd = mysqli_query($link, $request)){ $rlt['erreur'] = 'Erreur: Update not done'; } $link->close(); } die( json_encode($rlt) ); 

Hope this is a little close ...

0
source

Try the following:

// javascript

 var params = { id: _id, id_key: id_key, table: table, data: data }; // explicitely tells javascript you're expecting json response // with this $.post shortcut $.post('url', params, function(data) { //console.log(data) if (data.erreur) { // Error occured } else { // no error } }, 'json'); 

// PHP

 <?php // You don't need to json_decode the $_POST data, but you can sanitize or // perform any validation check if you want $id = $_POST['id']; $id_key = $_POST['id_key']; $table = $_POST['table']; $data = $_POST['data']; // Your process here echo json_encode($rlt); exit; 

Hope this helps.

0
source

All Articles