【问题标题】:Send REST response on data using CakePhp使用 CakePhp 发送数据的 REST 响应
【发布时间】:2015-12-08 08:28:27
【问题描述】:

您好,我在构建服务时遇到问题。现在我可以在 json 中看到响应的正文和所有内容。但是一位使用$http.get('url'); 获得角度响应的朋友似乎很难获得数据。我不熟悉 angular,所以我不确定那个调用是如何工作的,我不确定他为什么不能得到我发送给他的数据。这是代码:

                    $jsonResponse = array(
                        'success' => 'Llamada exitosa.',
                        'producto' => $responseproducts
                    );

                    $this->response->type('json');

                    $this->set('data', $jsonResponse);
                    $this->set('_serialize', 'data');

                    $json = json_encode($jsonResponse);
                    $this->response->body($json);

$responseproducts 是我希望他得到响应的数组。当我在邮递员上尝试时,该 URL 确实返回了正文上的数组。我可以在一个简单的 html 上构建的 ajax 上使用数据来查看是否可以使用 url。但是由于某种原因,我的朋友无法获取响应数据的数组。有什么想法我可能做错了吗?

【问题讨论】:

    标签: php angularjs rest cakephp


    【解决方案1】:

    呵呵,我找到了答案。再挖掘一点,似乎他对 CORS 的可用性有疑问。我只是使用我发现的下一个代码让 CORS 进入我的服务:

    //Enabling CORS for CakePHP
        public function beforeFilter() {
            parent::beforeFilter();
                $this->response->header('Access-Control-Allow-Origin','*');
                $this->response->header('Access-Control-Allow-Methods','*');
                $this->response->header('Access-Control-Allow-Headers','X-Requested-With');
                $this->response->header('Access-Control-Allow-Headers','Content-Type, x-xsrf-token');
                $this->response->header('Access-Control-Max-Age','172800');
        }
    

    当我在找到此代码的页面上阅读时,并非所有 5 个都是必需的。大多数情况下,它仅适用于第一个,但如有必要,请测试其余部分。希望这有助于soemone:P

    【讨论】:

      猜你喜欢
      • 2013-10-23
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      相关资源
      最近更新 更多