How to return the correct content-type for JSON in CakePHP?

前端 未结 5 585
别那么骄傲
别那么骄傲 2021-02-04 13:24

I\'m trying to set the content-type header for a JSON response accessed with an AJAX GET request. I\'ve followed tutorials on blogs and the bakery but I always receive \'text/ht

5条回答
  •  时光取名叫无心
    2021-02-04 13:57

    I've been having the same problem as the original poster, and what worked for me was to follow Rob Wilkerson's advice, but also make sure I was using

    jQuery.ajax()
    

    instead of

    jQuery.get()
    

    or

    jQuery.post()
    

    jQuery.ajax() allows you to set the dataType to 'json' whereas the other two don't seem to allow you to set the datatype at all. When I set the data type in the AJAX request to 'json' it all worked as it should.

提交回复
热议问题