I just came across a very strange behavior of jQuery with Laravel API backend. Don't worry, not the normal issue of authentication etc. Two Scenarios
1: No output with jQuery
public function index(Request $request)
{
$items = $this->repository->get();
return response()->json(["data" => $items]);
}
2: Output with jQuery
public function index(Request $request)
{
$items = $this->repository->get();
echo ".";
return response()->json(["data" => $items]);
}
jQuery Call:
$.ajax({
url: url,
type: 'GET',
async: false, // tested with and without
cache: false, // tested with and without
ontentType: 'application/json', // tested with and without
beforeSend: function beforeSend(xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
},
dataType: 'text', //json
success: function success(data) {
debugger;
},
error: function error(xhr, status, _error2) {
debugger;
}
});
Interesting, that I don't get in Scenario two just the dot, I get everything with I tested with all different kinds of jQuery dataTypes, accept-header and so on. Next, I tested different routes, all the same. Further, Swagger and Postman show in both ways the data as expected. I use Chrome 80.0.3987 Official 64bit.
Any ideas on how to handle this?
Edit: If I use the following, I also see both results (two times the same). This can't be the solution.
$items = $this->repository->get();
echo($items->toJson(JSON_PRETTY_PRINT));
return response()->json(["data" => $items]);