Jquery Ajax call works with HTML but not JSON -
i have started work drupal 8 , doing ajax calls server using fresh controller system implemented drupal team.
my problem occurs when have defined route , make ajax call using jquery. if set datatype "json" receive 404, changing "html" work.
the function route points looks this:
function createresponse(){ return new jsonresponse(['data' => 'test']); }
my response/request-headers this:
response
cache-control must-revalidate, no-cache, post-check=0, pre-check=0, private connection keep-alive content-encoding gzip content-language en content-type application/json date wed, 19 nov 2014 12:20:35 gmt expires sun, 19 nov 1978 05:00:00 gmt keep-alive timeout=10 server nginx transfer-encoding chunked vary accept-encoding x-powered-by php/5.5.18-1~dotdeb.1 x-ua-compatible ie=edge,chrome=1
request header
accept application/json, text/javascript, */*; q=0.01 accept-encoding gzip, deflate accept-language en-us,en;q=0.5 content-length 9 content-type application/json; charset=utf-8 host d8test.local referer http://d8test.local/ user-agent mozilla/5.0 (windows nt 6.3; wow64; rv:33.0) gecko/20100101 firefox/33.0 x-requested-with xmlhttprequest
is drupal 8 specific? there stupid variable forbidding json xmlhttprequests? can access route surfing route , can data using html need in json-format.
this jquery code:
jquery.ajax({ url: drupalsettings.path.basepath + "testajax", type: "post", contenttype: "application/json; charset=utf-8", datatype: "json", data: {'type' : 'list'}, success: function(data){ console.log(data); } });
testajax route have defined, works fine , work if change datatype "html" feels weird.
thanks vital clues spokey in comments found out problem, route wrongly setup in drupal. instead of _controller used _content. _controller accesses function without adding lots of markup-overhead.
so if bump problem yourself, make sure routes.yml file has _controller setting , not _content one.
Comments
Post a Comment