如何使用JQuery和Laravel从数据库中删除

时间:2015-04-28 15:26:24

标签: javascript php jquery laravel

我有以下代码和if语句,具体取决于用户是否保存了文章。我只是尝试使用jquery从数据库中删除文章。我不确定我哪里出错了?非常感谢帮助!

查看:

<form action="{{URL::route('article-delete')}}" method="post" id="article_one_delete">
<div class="form-group">
<input type="hidden" name="first_desc" value="{{$firstrow->description}}" class="form-control">
</div>
<div class="form-group">
<input type="hidden" name="first_title" value="{{$firstrow->title1}}" class="form-control">
</div>

<button type ="button" id="Recodelete" class="btn btn-success btn-xs">UnSave</button>
{{Form::token()}}

</form>

路线:

Route::delete('/home/', array( 'as' => 'article-delete', 
'uses' => 'HomeController@deletearticle'));

控制器:

public function deletearticle(){

 $firsttitle       = Input::get('first_title');

$articledelete  = UserSaveArticle::where('user_id', Auth::id()
 ->where ('user_save_articles.chosen_title', $firsttitle))->delete();

return true;

JQuery的:

$(document).ready(function(){
    $('#Recodelete').on('click', function(){
       var article_one_delete = $('#article_one_delete').serializeArray();
       var url_d         = $('#article_one_delete').attr('action');

       $.get(url_d, article_one_delete, function(data){
           console.log(data);
       });
    });
 });

1 个答案:

答案 0 :(得分:1)

  1. 您应该为DELETE文章定义正确的路线,如下所示:
  2. password = b'bXlwYXNzd29yZCE=' #this is b64, pretend its stronger
    password = decode(password)  # fake func for illustration
    
    authorization = 'NLAuth nlauth_account=1111111,nlauth_email=' + username + ',nlauth_signature=' + password + ',nlauth_role=' + role
    req = urllib.request.Request(url)
    req.add_header('Authorization', authorization)
    req.add_header('Content-Type', 'application/json')
    req.add_header('Accept', 'application/json')
    
    logging.debug(req.headers)
    
    1. Route::delete('/article/{id}', ['as' => 'article-delete', 'uses' => 'HomeController@deleteArticle']); $ id变量(文章ID)将作为方法参数提供:
    2. HomeController
      1. 在PHP方面你定义了DELETE路由,这意味着你应该使用ajax方法在JS端发出DELETE请求:
      2. function deleteArticle($id)
        {
            …
        }