2

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

したがって、コミュニケーションはありますが、望ましい結果ではありません。私は何を間違っていますか?

4

1 に答える 1

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
});

これがあなたに役立つことを願っています

于 2013-05-08T13:53:22.050 に答える