Returning JSON from a PHP Script

前端 未结 18 1444
南方客
南方客 2020-11-21 04:59

I want to return JSON from a PHP script.

Do I just echo the result? Do I have to set the Content-Type header?

18条回答
  •  忘了有多久
    2020-11-21 05:28

    While you're usually fine without it, you can and should set the Content-Type header:

    If I'm not using a particular framework, I usually allow some request params to modify the output behavior. It can be useful, generally for quick troubleshooting, to not send a header, or sometimes print_r the data payload to eyeball it (though in most cases, it shouldn't be necessary).

提交回复
热议问题