我几乎找到了如何在Zend Framework 2中实现REST样式的方法。
但我的疑问是,像get(),getList()这样的方法工作正常,但update()方法id没有调用并在html页面中显示以下错误。
HTML PAGE:
$.ajax({
url: 'http://128.199.233.137/api/v1/tes/74',
data: {"gender":"1","country":"1"},
type: 'PUT',
success: function(result) {
console.log(result);
// Do something with the result
}
});
OPTIONS http://233.102.233.137/api/v1/tes/74 jquery-2.1.3.js:8625 jQuery.ajaxTransport.sendjquery-2.1.3.js:8161 jQuery.extend.ajaxput.html:16 (anonymous function)jquery-2.1.3.js:4430 jQuery.event.dispatchjquery-2.1.3.js:4116 jQuery.event.add.elemData.handle
put.html:1 XMLHttpRequest cannot load http://233.102.233.137/api/v1/tes/74. Invalid HTTP status code 405
我的控制人是:
<?php
namespace Tes\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
use Zend\Http\Response;
class TesController extends AbstractRestfulController
{
//getAction
public function get($id) {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT');
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type");
$resp = array("method" => "Get", "id" => $id);
return new JsonModel($resp);
}
//updateAction
public function update($id, $data) {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT');
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type");
$resp = array("method" => "Update", "id" => $id, "data" => $data);
return new JsonModel($resp);
}
}