我现在有一个 CodeIgniter 问题:我使用REST 控制器库(这真的很棒)创建一个 API,但我无法收到 PUT 请求...
这是我的代码:
function user_put() {
$user_id = $this->get("id");
echo $user_id;
$username = $this->put("username");
echo $username;
}
我使用curl来发出请求:
curl -i -X PUT -d "username=test" http://[...]/user/id/1
user_id 已满,但用户名变量为空。但它与动词 POST 和 GET 一起使用。
请问你有什么想法吗?
谢谢 !
根据:http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/我们应该咨询https://github.com/philsturgeon/codeigniter-restserver/blob/master/application/libraries/REST_Controller.php#L544看到这个方法:
/**
* Detect method
*
* Detect which method (POST, PUT, GET, DELETE) is being used
*
* @return string
*/
protected function _detect_method() {
$method = strtolower($this->input->server('REQUEST_METHOD'));
if ($this->config->item('enable_emulate_request')) {
if ($this->input->post('_method')) {
$method = strtolower($this->input->post('_method'));
} else if ($this->input->server('HTTP_X_HTTP_METHOD_OVERRIDE')) {
$method = strtolower($this->input->server('HTTP_X_HTTP_METHOD_OVERRIDE'));
}
}
if (in_array($method, array('get', 'delete', 'post', 'put'))) {
return $method;
}
return 'get';
}
查看我们是否定义了 HTTP 标头HTTP_X_HTTP_METHOD_OVERRIDE
它使用它来支持我们在网络上实现的实际动词。要在请求中使用它,您需要指定标头X-HTTP-Method-Override: method
(so X-HTTP-Method-Override: put
) 生成自定义方法覆盖。有时框架期望X-HTTP-Method
代替X-HTTP-Method-Override
所以这因框架而异。
如果您通过 jQuery 执行此类请求,则可以将此块集成到您的 ajax 请求中:
beforeSend: function (XMLHttpRequest) {
//Specify the HTTP method DELETE to perform a delete operation.
XMLHttpRequest.setRequestHeader("X-HTTP-Method-Override", "DELETE");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)