Slim PUT返回NULL

时间:2013-05-08 12:10:27

标签: php jquery http put slim

我有Slim Framework和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"
}

所以沟通在那里,但不是理想的结果。我做错了什么?

1 个答案:

答案 0 :(得分:3)

首先你应该检查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
});

我希望这可以帮到你