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

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

问题描述



我现在可以通过以下方式自动提供JSON内容:此CakePHP 指南

解决方案

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


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




    • 路由添加 Router :: parseExtensions('json'); php 配置文件。这告诉Cake接受 .json URI扩展

    • 添加 RequestHandler 您将要使用的控制器中的组件列表

    • Cake在这里变得聪明,现在为正常请求和JSON / XML等请求提供不同的视图,如果需要,如何返回这些结果。现在,您应该可以通过以下方式访问控制器中的操作:

      • 使用URI / controller / action 将使用 /view/controller/action.ctp )中的视图,或

      • 使用URI controller / action.json (将使用 /view/controller/json/action.ctp 中的视图)



  2. 如果您不想定义这些视图,进一步处理,并且响应准备好了,你可以告诉CakePHP忽略这些视图,并立即使用 _serialize 返回数据。使用 _serialize 会告诉Cake以正确的格式(XML,JSON等)格式化您的响应,设置标题并根据需要返回,而不需要执行任何操作文档)。要利用这个神奇的效果:




    • 设置要返回的变量和视图变量 $通过调用 > $ this-> set('_ serialize',array('posts')); ,其中参数是刚刚在上一行设置的视图变量


就是这样。所有标题和回复将被Cake接管。这只是让JSONP工作(文档):


  1. 通过设置 $ this-> set('_ jsonp',true); ,Cake会找到回调函数名参数,并格式化响应以使用该回调函数名。

所以,假设你设置了Cake接受 .json 请求,这是您的典型操作可以看起来像使用JSONP:

  public function getTheFirstPost()

$ post = $ this-> Post-> find('first');

$ this-> set(array(
'post'=> $ post,< - 设置视图中的帖子
'_serialize'=&数组('post'),< - 告诉蛋糕使用那个post
'_jsonp'=> true< - 并将其包装在回调函数中

);

和JS:

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


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

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

解决方案

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. Set up your views to accept/use JSON (documentation):

    • 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)
  2. 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:

    • 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

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

  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.

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
    )
);

And the 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天全站免登陆