I am facing an issue with my Laravel controller. The code is straightforward:
class MyController extends Controller
{
public function list()
{
return response()->json(['a' => 'hello']);
}
}
When I access the corresponding URL in Google Chrome, everything works as expected. I receive a response {"a":"hello"}
with the content type of application/json
.
However, when trying to fetch the data using JavaScript (utilizing the fetch polyfill), I notice that the content type shows up as text/html
. This discrepancy was verified both in Google Chrome DevTools and within my code:
fetch("/list")
.then(function(response) {
console.log(response.headers.get('Content-Type')); // => 'text/html'
return response.json();
})
.then(function(json) {
})
.catch(function(error){
});
Unfortunately, this situation leads to an error -
Unexpected token < in JSON at position 0
.
I am puzzled by what could be causing this.
PS:
Laravel version - 5.4, PHP Version 7.0.15, XAMPP 3.2.2
Fetch polyfill version - 2.0.3
Google Chrome version - 57.0.2987.133
Oddly enough, this problem seems to work correctly in Microsoft Edge v.20.10240.