我一直试图让Nodesagencys CakePHP Platform-API-plugin使用我的Cake应用程序几天,但现在没有成功。你能否指出我正确的方向?
当我通过jQuerys admin_add()
尝试发布到我的add()
或$.ajax
方法时,我只返回views/$Controller/json/admin_index.ctp
(或index.ctp
}, views/$Controller/json/admin_add.ctp template
。
请求似乎已经过身份验证,并且在开发工具中可以作为POST(200)查看,但无论我做什么,我只返回index.ctp视图,而不是admin_add。
继承人ajax链接:
$.ajax({
type: "POST",
url: "http://dev.mydomain.co.uk/faqArticles.json?token=" + userToken,
data: data,
dataType: "json",
继承了admin_add方法
public function admin_add() {
// New API code
$this->Api->denyPublic('add');
if ($this->request->is('post')) {
if ($this->request->is('api')) {
if ($this->request->is('json')) {
// Need to add the current auth'd users to this request data here
$this->request->data['user_id'] = $this->Auth->user('id');
// Get JSON encoded data submitted to a PUT/POST action
$data = $this->request->input('json_decode');
$this->FaqArticle->create();
if ($this->FaqArticle->save($data)) {
// Get the id of the just created post to pass back to client for future updates.
$caId = $this->FaqArticle->getInsertID();
$success = true;
} else {
$caId = null;
$success = false;
}
$this->set(compact('success', 'caId'));
}
} else {
// Normal CRUD stuff
$this->FaqArticle->create();
// Assigned logged in users to article
$this->request->data['FaqArticle']['user_id'] = $this->Auth->user('id');
if ($this->FaqArticle->save($this->request->data)) {
$this->Session->setFlash('The faq article has been saved', 'flash_success');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('The faq article could not be saved. Please, try again.', 'flash_failure');
}
}
}
$users = $this->FaqArticle->User->find('list');
$faqCategories = $this->FaqArticle->FaqCategory->find('list');
$this->set(compact('users', 'faqCategories'));
// Set layout
$this->layout = 'admin';
}
views / faqArticles / json / admin_index.ctp看起来像:
<?php
$this->set('success', true);
$this->set('data', "ADMIN INDEX");
echo json_encode(compact('success', 'data'));
?>
views / faqArticles / json / admin_add.ctp看起来像:
<?php
$this->set('success', true);
$this->set('data', "ADMIN ADD");
echo json_encode(compact('success', 'data'));
?>
来自每个POST的回复是:
{
"success":true,
"data":"ADMIN INDEX",
...
这应返回admin_add JSON视图,但按上述方式获取ADMIN INDEX ...
显然我做错了什么但是什么?任何帮助都会很棒。
谢谢
答案 0 :(得分:1)
当你发出Ajax请求时,我认为你只是链接到错误的页面(索引页而不是admin_add)我希望你的ajax请求看起来更像这样:
$.ajax({
type: "POST",
url: "http://dev.mydomain.co.uk/admin/faqArticles/add.json?token=" + userToken,
data: data,
dataType: "json"});