PUT与jquery返回错误

时间:2013-05-21 11:19:45

标签: php jquery html curl put

我正在尝试使用小型jquery脚本更新字段,但它一直返回错误,我无法看到我遇到问题的地方。

我有一个锚点onclick =“reportAd(MyAid)”运行它:

function reportAd(aid) {
   var conf = confirm("Will you report this ad?");

   if(conf == true) {         
      $.ajax({
         url: rootURL + '/reportad/',
         type: 'PUT',
         dataType: 'json',
         contentType: 'application/json',
         data: {'aid': ''+aid+''},
         success: function(data) {
             alert("Ad have been reported");
         },
         error: function(data) {
             console.log(data);
         }
     });
   }
   return false;
}

哪个应该运行:

$app->put('/reportad/', function() use ($app, $adverts) {    
   $request = Slim::getInstance()->request();
   $data = json_decode($request->getBody());

   $adverts->report_ad($data->aid);
});

FireBug给了我这个:

Object { readyState=4, status=200, statusText="OK"}

如果我用cURL

调用脚本
curl -i -X PUT -H 'Content-Type: application/json' -d '{"aid":"43"}' http://www.domain.dk/reportad/

它有效。

2 个答案:

答案 0 :(得分:1)

您要求将答案的内容解析为JSON。如果它不是JSON,那么你可能遇到了一个parseError。

尝试删除dataType参数。

答案 1 :(得分:0)

克服缺乏对“PUT”支持的标准方法是使用“POST”并引入伪PUT机制,服务器端代码可以在其上进行分支。

例如:

$.ajax({
    type: 'POST',
    data: {method: 'PUT', 'aid': ''+aid+''},
});

你所做的服务器端取决于你正在使用的语言/框架。