久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為 CakePHP 發送正確的 JSON 內容類型

Sending correct JSON content type for CakePHP(為 CakePHP 發送正確的 JSON 內容類型)
本文介紹了為 CakePHP 發送正確的 JSON 內容類型的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在我的 CakePHP 應用程序中,我返回 JSON 并針對某些請求退出.一個例子是嘗試訪問 API 以作為 GET 請求登錄:

In my CakePHP app I return JSON and exit for certain requests. An example of this would be trying to access the API for a login as a GET request:

header('Content-Type: application/json');
echo json_encode(array('message'=>'GET request not allowed!'));
exit;

但是,我必須在 echo 前面加上內容類型的前綴,才能將其作為 JSON 發送.否則我在另一端的代碼解釋不同.

However I am having to prefix the echo with the content type in order for it to be sent as JSON. Otherwise my code at the other end interprets it different.

關于如何解決這個問題的任何想法?或者至少改進它.

Any ideas on how to get around this? Or at least improve it.

更新:Cake 2.3.0 版

推薦答案

您可以利用新的 2.x 響應對象:

You can leverage the new 2.x response object:

public function youraction() {
    // no view to render
    $this->autoRender = false;
    $this->response->type('json');

    $json = json_encode(array('message'=>'GET request not allowed!'));
    $this->response->body($json);
}

參見 http://book.cakephp.org/2.0/en/controllers/request-response.html#cakeresponse

您也可以使用強大的休息功能和 RequestHandlerComponent 來自動實現這一點:http://book.cakephp.org/2.0/en/views/json-and-xml-views.html

Also you could use the powerful rest features and RequestHandlerComponent to achieve this automatically as documented: http://book.cakephp.org/2.0/en/views/json-and-xml-views.html

您只需要允許擴展 json 并將您的操作稱為 /controller/action.json.然后 cake 將自動使用 JsonView,您只需將數組傳入.視圖類會將其轉換為 JSON 和有效響應.

You just need to allow the extension json and call your action as /controller/action.json. Then cake will automatically use the JsonView and you can just pass your array in. It will be made to JSON and a valid response by the view class.

這兩種方式都比您的退出"解決方案更簡潔 - 嘗試對包含 die()/exit() 的代碼進行單元測試.這將悲慘地結束.所以最好一開始就不要在你的代碼中使用它.

Both ways are cleaner than your "exit" solution - try to unit-test code that contains die()/exit(). This will end miserably. So better never use it in your code in the first place.

這篇關于為 CakePHP 發送正確的 JSON 內容類型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 免费黄色在线观看 | 99视频免费看 | 久在线 | 狠狠插天天干 | 日韩 国产 在线 | 欧美亚洲成人网 | v片网站| 久久精品手机视频 | 99精品视频在线观看 | 男女激情网站免费 | 91免费电影 | 欧美午夜一区 | 久久久久国产 | 久久久高清 | 国产农村妇女精品一区 | 在线一区视频 | 日韩乱码av | 精品成人一区二区 | 中文在线一区二区 | 99免费看 | 97在线观看| 亚洲一级毛片 | 成人影院网站ww555久久精品 | 激情婷婷 | 久久久久久久av麻豆果冻 | 99久久精品免费看国产四区 | 老牛嫩草一区二区三区av | 国产乱码精品一区二区三区五月婷 | 在线成人免费视频 | 日韩aⅴ片| 四虎在线观看 | 国产第一区二区 | 在线亚洲免费视频 | 久久精品屋 | 色888www视频在线观看 | 亚洲国产精品一区二区三区 | 91久久久久久久久久久久久 | 日韩久久网 | 精品视频亚洲 | 国产精品一区二区三区久久 | 亚洲精品一区国产精品 |