i have quite simple laravel controller:
class mycontroller extends controller { public function list() { return response()->json(['a' => 'hello']); } }
when try open corresponding url in browser(i use google chrome) works fine. there {"a":"hello"}
response , content type application/json
.
but when receive data via javascript (i use fetch polyfill) see content type text/html
. checked in googlechrome devtools , within code:
fetch("/list") .then(function(response) { console.log(response.headers.get('content-type')); // => 'text/html' return response.json(); }) .then(function(json) { }) .catch(function(error){ });
well i've got error - unexpected token < in json @ position 0
.
i don't understand what's happened.
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
and yes, sounds strange, in ms edge v.20.10240 works fine.
you need set header of request content type of json. dont know polyfill how jquery
$.ajax({ type: 'get', url: url, contenttype: 'application/json; charset=utf-8', success: error: });
Comments
Post a Comment