php - Slim PUT 返回 NULL

标签 php jquery http put slim

我的 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

响应 header 如下所示:

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"
}

所以沟通是存在的,但没有达到预期的结果。我做错了什么?

最佳答案

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

希望这对你有帮助

关于php - Slim PUT 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16440344/

相关文章:

javascript - 嵌入的 js 文件不适用于 jQuery .load() 内容

jquery - Bootstrap div 在窗口调整大小时重叠

wcf - BasicHttpBinding 支持 HTTP 和 HTTPS,无需更改配置

JavaScript 未在 Rails 回调中执行?

c# - HttpWebRequest 序列

linux - mp3 http 流 : recording and playing simultaneosly

php - 路由到 View 而不传递变量 - laravel

Javascript 发布数据

php - 如何在 PHP 中通过 PDO 循环执行 MySQL 查询?

php - Mysqli 获取数组不工作