2014-03-19 28 views
0

嗨,我有一个请求放把CakePHP一个简单的jQuery代码CakePHP REST API - 放入和删除不起作用?

$("#putBtn").click(function(){ 
    var id = $("#searchTxt").val(); 
    $.ajax({ 
     url: 'http://localhost/cakephp/recipes/'+id, 
     type: 'PUT', 
     async: false, 
     cache: false, 
     dataType:'json', 
     data:{ 
      name:"777", 
      number:"777" 
     }, 
     success: function(data) { 
      console.log(data); 
     }, 
     error: function(textStatus, errorThrown) { 
      console.log("error"); 
     } 
    }); 
}); 

问题是,当运行这个命令,并将其发送给CakePHP的 它使这个

/CakePHP的/食谱的错误/ 1。请求的资源上没有“Access-Control-Allow-Origin”标题。因此不允许原产地'null'访问。 jQuery的2.1.0.js:8556 错误

在我的CakePHP我只要按照本教程 //book.cakephp.org/2.0/en/development/rest.html

public function edit($id) { 
    header("Access-Control-Allow-Origin: *"); 
    header('Content-Type: application/json'); 
    header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE"); 
    header("Access-Control-Allow-Headers: Content-Type"); 
    $this->viewClass = 'json'; 
    if ($this->Recipe->save($this->data)) { 
     $message = 'Saved'; 
    } else { 
     $message = 'Error'; 
    } 
    $this->set(array(
     'message' => $message, 
     '_serialize' => array('message') 
    )); 
} 

我也加入

Router::resourceMap(array(
    array('action' => 'index', 'method' => 'GET', 'id' => false), 
    array('action' => 'view', 'method' => 'GET', 'id' => true), 
    array('action' => 'add', 'method' => 'POST', 'id' => false), 
    array('action' => 'edit', 'method' => 'PUT', 'id' => true), 
    array('action' => 'delete', 'method' => 'DELETE', 'id' => true), 
    array('action' => 'update', 'method' => 'POST', 'id' => true) 
)); 

Router::mapResources('recipes'); 
Router::parseExtensions(); 

我不知道为什么CakePHP是不接受我的PUT请求或DELETE命令router.php。 有什么建议或建议? 。或者我做错了什么。 thx提前。

+1

您不能发送XHR请求到不同的:其他组合了一个存在于网页的网址。我猜测网页的网址有一个非默认的端口80 .. –

+0

'url:'http:// localhost/cakephp/recipes /'+ id,'< - 使用相对网址。 – AD7six

+0

如果url位于不同的IP地址,该怎么办? – user3060549

回答

1

我遇到了类似的问题,CakePHP版本2.5.4解析PUT调用。

不优雅,但这并工作。

前端代码:

$("#myButton").click(function() { 

    $.ajax({ 
      type  : 'PUT', 
      url  : '/projects/foo', 
      dataType : 'json', 
      data  : JSON.stringify({ 
       'projectId' : 789, 
       'desc'  : "cheese cake", 
      }), 
      success: function(return_data) { 
      alert("success"); 
      console.log(JSON.stringify(return_data)); 
      }, 
      error: function(return_data) { 
      alert("error"); 
      console.log(JSON.stringify(return_data)); 
      }); 
}); 

后端代码(应用程序/控制器/ ProjectsController.php)

public function foo() { 

     $this->autoRender = false; 

     if($this->request->is('PUT')) { 

      $in = $this->request->data; 
      reset($in); 
      $in = json_decode(key($in), true); 

      $project_id = $in['projectId']; 
      $desc = $in['desc']; 
     } 

     $response = array(
      'project_id' => $project_id, 
      'desc'   => $desc, 
      ); 

     echo json_encode($response); 
     exit; 
} 

结果:

检查您的控制台当你测试这个代码。它只会发回您首先发送的内容,但会保证CakePHP中的后端代码现在可以解析这些JSON PUT请求。