Zend框架2 - Rest api中的问题

时间:2015-03-24 07:04:40

标签: rest zend-framework2

我几乎找到了如何在Zend Framework 2中实现REST样式的方法。

但我的疑问是,像get(),getList()这样的方法工作正常,但update()方法id没有调用并在html页面中显示以下错误。

HTML PAGE:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Login</title>
<script src="jquery-2.1.3.js"></script>
<script>
   $(document).ready(function(){                     
      $(function(){
         $("#sb").click(function(e){

         alert("put method");
            e.preventDefault();


        $.ajax({
        type: 'PUT',
        url: 'http://128.199.233.137/api/v1/tes/74',
        data: {"gender":"1","country":"1","state":"2","city":"23","address1":"address123","address2":"address2","postal_code":"627811","birthdate":"11-12-2045","phone":"0442805565","mobile":"9865521557","blood_group":"o-","weight":"60","height":"5.2","bmi":"10"},
        dataType: "json",
        contentType: "application/json",
        success: function(result) {

        console.log(result);

            // Do something with the result
        }
    });


        });
      });
    });
</script>
</head>

<body>

<div  id = "sb">asdf</div>
</body>
</html>


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

}

1 个答案:

答案 0 :(得分:0)

可能是您的服务器未正确配置以处理POSTGET以外的请求。有时,服务器配置被设置为拒绝所有其他http动词,这可能是您获得405 - Method not allowed响应的原因。

所以问题是:

1:您使用的是什么网络服务器?

2:是您的服务器配置为处理所有请求谓词。