我有一个Slim框架和一个PUT请求的问题。我有一个小jQuery脚本,这将更新到期时间时,一个按钮被点击。
$("#expiry-button").click(function(event) {
event.preventDefault();
$.ajax({
url: 'http://www.domain.com/expiry/38/',
dataType: 'json',
type: 'PUT',
contentType: 'application/json',
data: {aid:'38'},
success: function(){
var text = "Time updated";
$('#expiry').text(text).addClass("ok");
},
error: function(data) {
var text = "Something went wrong!";
$('#expiry').text(text).addClass("error");
}
});
});我总是听到“出了问题!”
在我配置了Slim的index.php中,我有以下内容
$app->put('/expiry/:aid/', function($aid) use($app, $adverts) {
$id = $app->request()->put($aid);
$adverts->expand_ad_time($id["aid"]);
}); 如果我执行var_dump($id),得到的结果为NULL
响应头如下所示:
Status Code: 200
Pragma: no-cache
Date: Wed, 08 May 2013 12:04:16 GMT
Content-Encoding: gzip
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze15
Vary: Accept-Encoding
Content-Type: text/html; charset=utf-8
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Transfer-Encoding: chunked
Connection: Keep-Alive
Keep-Alive: timeout=15, max=100
Expires: Thu, 19 Nov 1981 08:52:00 GMT和请求正文
Request Url: http://www.domain.com/expiry/38/
Request Method: PUT
Status Code: 200
Params: {
"aid": "38"
}因此,沟通是存在的,但不是预期的结果。我做错了什么?
发布于 2013-05-08 21:53:22
首先应该检查json数据,因为它是无效的:http://jsonlint.com/尝试这样做:{"aid":"38"}
如果您需要JSON数据,那么我会这样做:
$app->put('/expiry/:aid/', function($aid) use($app, $adverts) {
// Decode the request data
$test = json_decode($app->getInstance()->request()->getBody());
echo $test->aid; // from the JSON DATA
}); 如果您想从url /expiry/38/中获取数字,则可以从传递给函数的变量$aid中获取该数字
$app->put('/expiry/:aid/', function($aid) use($app, $adverts) {
echo $aid; // from the url
});我希望这能帮助你
https://stackoverflow.com/questions/16440344
复制相似问题