REST API in PHP output to JSON file extension

前端 未结 3 1401
走了就别回头了
走了就别回头了 2020-12-29 16:39

So I want to write a REST API in PHP for JSON for consumption on the iPhone, but also a lot of websites and devices. I have the below code, when accessed via a GET statemen

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 16:57

    The problem is this line:

    header("Content-type: application/json");
    

    I know this looks like the right way to do (after all, application/json is the official MIME type for JSON content), but it causes most browsers to present you with a file download dialog, when you just want to see the text. You can use the text/plain encoding to avoid this. Note that your AJAX/iPhone/... app probably doesn't care about the content type, so your API will work in both cases.

    Also see this blog post which provides some more context.

提交回复
热议问题