使用 CakePHP 提供 JSONP 的正确方法是什么? [英] What's the proper way to serve JSONP with CakePHP?

查看:18
本文介绍了使用 CakePHP 提供 JSONP 的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 CakePHP 提供 JSONP 内容,并且想知道这样做的正确方法是什么.

I want to serve JSONP content with CakePHP and was wondering what's the proper way of doing it so.

目前我可以按照这个 CakePHP 指南自动提供 JSON 内容.

Currently I'm able to serve JSON content automatically by following this CakePHP guide.

推荐答案

我还没有找到关于如何使用 CakePHP 2正确 返回 JSONP 的完整示例,所以我将写下来.OP 要求正确的方法,但他的回答没有使用 2.4 中现在可用的本机选项.对于 2.4+,这是正确的方法,直接来自他们的文档:

I've as yet not found a complete example of how to correctly return JSONP using CakePHP 2, so I'm going to write it down. OP asks for the correct way, but his answer doesn't use the native options available now in 2.4. For 2.4+, this is the correct method, straight from their documentation:

  1. 设置您的视图以接受/使用 JSON (文档):

  • Router::parseExtensions('json'); 添加到您的 routes.php 配置文件中.这告诉 Cake 接受 .json URI 扩展
  • RequestHandler 添加到您将要使用的控制器中的组件列表
  • Cake 在这方面变得很聪明,现在可以为您提供针对普通请求和 JSON/XML 等请求的不同视图,让您可以在需要时灵活地返回这些结果.您现在应该可以通过以下方式访问控制器中的操作:
    • 使用 URI /controller/action(将使用 /view/controller/action.ctp 中的视图),或
    • 使用 URI /controller/action.json(将使用 /view/controller/json/action.ctp 中的视图)
    • Add Router::parseExtensions('json'); to your routes.php config file. This tells Cake to accept .json URI extensions
    • Add RequestHandler to the list of components in the controller you're going to be using
    • Cake gets smart here, and now offers you different views for normal requests and JSON/XML etc. requests, allowing you flexibility in how to return those results, if needed. You should now be able to access an action in your controller by:
      • using the URI /controller/action (which would use the view in /view/controller/action.ctp), OR
      • using the URI /controller/action.json (which would use the view in /view/controller/json/action.ctp)

      如果你不想定义那些视图,即你不需要做任何进一步的处理,并且响应已经准备好了,你可以告诉 CakePHP 忽略这些视图并立即使用_serialize.使用 _serialize 将告诉 Cake 以正确的格式(XML、JSON 等)格式化您的响应,设置标头并根据需要返回它,而您无需执行任何其他操作(文档).要利用这个魔法:

      If you don't want to define those views i.e. you don't need to do any further processing, and the response is ready to go, you can tell CakePHP to ignore the views and return the data immediately using _serialize. Using _serialize will tell Cake to format your response in the correct format (XML, JSON etc.), set the headers and return it as needed without you needing to do anything else (documentation). To take advantage of this magic:

      • 像设置视图变量一样设置要返回的变量,即 $this->set('post', $post);
      • 通过调用$this->set('_serialize', array('posts'));告诉Cake将其序列化为XML、JSON等,其中参数是视图变量你只是在上一行设置
      • Set the variables you want to return as you would a view variable i.e. $this->set('post', $post);
      • Tell Cake to serialize it into XML, JSON etc. by calling $this->set('_serialize', array('posts'));, where the parameter is the view variable you just set in the previous line

      就是这样.Cake 将接管所有标头和响应.这只是让 JSONP 开始工作(文档):

      And that's it. All headers and responses will be taken over by Cake. This just leaves the JSONP to get working (documentation):

      1. 通过设置 $this->set('_jsonp', true); 告诉 Cake 将请求视为 JSONP 请求,然后 Cake 会去寻找回调函数名称参数,并格式化响应以使用该回调函数名称.从字面上看,设置一个参数可以为您完成所有工作.
      1. Tell Cake to consider the request a JSONP request by setting $this->set('_jsonp', true);, and Cake will go find the callback function name parameter, and format the response to work with that callback function name. Literally, setting that one parameter does all the work for you.

      因此,假设您已将 Cake 设置为接受 .json 请求,这就是使用 JSONP 时的典型操作:

      So, assuming you've set up Cake to accept .json requests, this is what your typical action could look like to work with JSONP:

      public function getTheFirstPost()
      
          $post = $this->Post->find('first');
      
          $this->set(array(
              'post' => $post,                 <-- Set the post in the view
              '_serialize' => array('post'),   <-- Tell cake to use that post
              '_jsonp' => true                 <-- And wrap it in the callback function
          )
      );
      

      还有 JS:

      $.ajax({
          url: "/controller/get-the-first-post.json",
          context: document.body,
          dataType: 'jsonp'
      }).done(function (data) {
          console.log(data);
      });
      

      这篇关于使用 CakePHP 提供 JSONP 的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆