Slim PUT返回NULL [英] Slim PUT returns NULL

查看:123
本文介绍了Slim PUT返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Slim Framework和PUT请求的问题。我有一个小小的jQuery脚本,它会在点击一个按钮时更新到期时间。

I have a problem with the Slim Framework and a PUT request. I have a litte jQuery script that that will update an expiry time when a button is clicked.

$("#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中,我有这个

In my index.php where I have configured Slim, I have this

$app->put('/expiry/:aid/', function($aid) use($app, $adverts) {
    $id = $app->request()->put($aid);
    $adverts->expand_ad_time($id["aid"]);
}); 

如果我 var_dump($ id) I获取NULL

if I var_dump($id) I get 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"
}

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

So the communication is there, but not the desired result. What am I doing wrong?

推荐答案

首先你应该检查一下json数据,因为它无效: http://jsonlint.com/
试试这个:{aid:38}

First you should check that json data, because it isn't valid: http://jsonlint.com/ Try with this: {"aid":"38"}

如果您需要JSON数据而不是我会这样做:

If you need that JSON data than i would do something like this:

$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获得它,你已经传递给函数

If you want the number from the url /expiry/38/ then, you can get it from the variable $aid, which you've passed to the function

$app->put('/expiry/:aid/', function($aid) use($app, $adverts) {
    echo $aid; // from the url
});

我希望这可以帮到你

这篇关于Slim PUT返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆