The fact is that in response I get the headers and json himself. It looks like this

return response()->json([ 'somedata' => 1 ]); 

In js so

 $.get('/page', function(data) { console.log(data) }); 

Result:

 HTTP/1.0 200 OK Cache-Control: no-cache Content-Type: application/json {"somedata":"1"} 

Accordingly, js cannot parse this. It was not possible to google. Yes, and it seems that before this behavior was not observed.

  • one
    console.log (data.body)? - Andrei Talanin
  • one
    those. data is not an object at all? - Andrei Talanin
  • one
    With other json requests the same situation? - Andrei Talanin
  • one
    And you say that it was not observed before: did it work in the same project and suddenly ceased, or what? - Andrei Talanin
  • one
    Let's continue the discussion in the chat . - Andrei Talanin

2 answers 2

The problem was in php7 typing.

 public function index() : string { return response()->json([ 'somedata' => 1 ]); } 

Without : string everything works great. Headers no longer fall into the response body.

    To decode json use jQuery.parseJSON

     var result = jQuery.parseJSON(data); for(i in result){ alert(result[i]); } 

    Result 1 in alert

    • It would not work, because there were headlines in the response body. - Fortael