Sending an ajax request to a php file:

$.ajax({ url: '/engine/search.php', dataType: 'json', data: { data:data }, method: 'POST', success: function(){ console.log('Request sent successfully. Pending...') } }).fail(function(response){ console.log(response); }).done(function(){ console.log('OK'); // "OK" не выводится }); 

PHP:

 <?php require('functions.php'); if(isAjax()) { $data = $_POST['data']; if(isset($data) && !empty($data)) { if(!preg_match('/[AZ][AZ][AZ]/', $data['from']) && !preg_match('/[AZ][AZ][AZ]/', $data['to'])) { header('HTTP/1.0 500 Internal Server Error'); die('Bad IATA codes provided'); } if(!validateDate($data['there']) && !validateDate($data['thence'])) { header('HTTP/1.0 500 Internal Server Error'); die('Bad date format provided'); } if(!preg_match('/^[1-9]*$/', $data['adults']) && !preg_match('/^[0-9]*$/', $data['teens']) && !preg_match('/^[0-9]*$/', $data['kids'])) { header('HTTP/1.0 500 Internal Server Error'); die('Bad passengers data provided'); } session_start(); $_SESSION['search_data'] = json_encode($data); } } else { header('HTTP/1.0 403 Forbidden'); die('Access denied'); } ?> 

It successfully processes the received data, but for some reason the ajax request comes in fail (), although the code is 200 (OK). What could be the problem?

  • And try to remove done() and fail() in js and transfer that code to success , error and complete callbacks. There, the matter is that success is an analogue of the same done() , and it is possible if the framework code sees that it has passed handlers in the settings array, it will go along the way of processing there, thus ignoring the handlers that you hung after the point. - Stanislav Belichenko February
  • one
    @ Stanislav is not, success and error are responsible for the state of the connection, but fail () and done () for an error or success as a result of the end of the script. But I still figured out the problem - I had a dataType, which was waiting for a response from the server in the form of a json-array, but did not receive it. All from damn inattention :) - JamesJGoodwin
  • Then write your own answer and mark it true, which I can say) Thank you for the information, I didn’t know - I never used done() and so on. - Stanislav Belichenko
  • I checked your words about fail and done - to be honest, I didn’t understand, after all, what you meant. Can I have more details? Because my fail and error work exactly the same way, and in fact we have the same context - the connection - so errors and success are the errors and successes of the connection. - Stanislav Belichenko February
  • Here is what I found in the documentation: === Deprecation Notice: The jqXHR.success (), jqXHR.error (), and jqXHR.complete () callbacks are removed as of jQuery 3.0. You can use jqXHR.done (), jqXHR.fail (), and jqXHR.always () instead. === So this is the same thing, just for the moment there are two options for obtaining this information. - Stanislav Belichenko February

1 answer 1

In the body of the ajax request, dataType: 'json' was specified, because of which ajax was waiting for a response from the server as a json array, but did not receive it.

  • faithful, echo "[]"; needs a bidet to help - Eugene Bartosh