There is a certain form from which certain data is sent, sent through the $.ajax() function:

 f.on('submit', function (e) { var d = f.serialize(); e.preventDefault(); $.ajax({ url: u, type: 'POST', data: d, dataType: 'json', beforeSend: b, error: function (jqXHR) { // }, success: function (data, textStatus, jqXHR) { // внимание сюда, отдельно радует наличие textStatus и jqXHR.statusText alert(jqXHR.statusText); } }); }); 

In my opinion, it is more accurate to send messages about the status of answers in a specially arranged place for this, in jqXHR.statusText , and leave the data either empty or fill it with real data.

In this case, we will not have confusion with the content of the answers, when there is something json , then text , which perhaps should not be displayed in plain text, but wrapped in meaningless json , for the sake of code unification.

This can be important when we use the standard functions for submit processing of forms, when all types of data received and sent should be reduced to a single form. Accordingly, in the controller we (try) do something like this:

 public function contactUs(ContactUsRequest $request) { // что-нибудь return response(json_encode([... массив / пустота ...])) ->header('Content-Type', 'application/json') ->header('charset', 'utf-8') ->setStatusCode(201, 'Сообщение успешно отправлено.') ; } 

The problem is that when you set the encoding in this way, it only affects the output массив / пустота (I checked), but the statusText does not, does not affect, we get the usual abracadabra, which usually happens when utf-8 processed incorrectly.

So, the question: what could be the problem? I personally have three options:

  1. Bug Laravel.
  2. Restriction of the standard for displaying messages about the status of http-responses.
  3. I miss something and do wrong.

Why do I need such code? In general, there is no need, just studying the subtleties.

    1 answer 1

    It seems he found the answer to your question here . There are two reasons not to do as I described, that is, not to use utf-8 in the response status from the server:

    1. The encoding of this text is not defined, and therefore it is useless to use non-ASCII characters.
    2. In HTTP / 2, this text is not used in principle, so sooner or later it will have to be abandoned.