简体   繁体   中英

laravel ajax post issue

I have been working on this all weekend and cant get it to work. I can get it working using get but not post. Im using Laravel 4 and jquery.

My JS looks like this:

$('.status').on('click', function(){
var $modal = $('#ajax-modal');
var id = $(this).attr("id");
setTimeout(function(){
 $modal.load('../status/'+id, '', function(){
 $modal.modal();
});
});
});

which opens a bootstrap modal just fine and loads the status page. On that page, I set a button with an id of the user, and when that is clicked, I call another JS snippet:

$modal.on('click', '.update', function(){
$modal
    .find('.modal-body')
    .html('<div class="progress progress-striped active"><div class="bar" style="width: 100%;">Processing ...</div></div>'); 
    var PostData = 'UserId='+$(this).attr("id");
    $.ajax({
        type: "POST",
        url: "",
        data:PostData,
        success: function(msg){
            $('.update').prop('disabled', true);
            setTimeout(function(){
            $modal
            .find('.modal-body')
            .html('<div class="alert-show-success"><img src="../../../public/assets/img/icons/accept.png" />This user was successfully de-activated!</div>');}, 1500);    
        },
        error: function(){
        //alert("failure");
        setTimeout(function(){
        $modal
        .find('.modal-body')
        .html('<div class="alert-show-failed"><img src="../../../public/assets/img/icons/failed.fw.png" />There was an error processing this request!</div>');}, 1500);
        }
        }); 

});

The modal loads fine, and it finds the status page fine, but nothing actually happens in the controller: (I hard coded the 2 in there to test it.

public function postStatus()
{
    DB::table('users')
    ->where('id', 2)
    ->update(array('activated' => 0));
}

Not sure what I am missing. Any help is greatly appreciated.

Try like this:

First, make your route named for URLs to be more dynamic:

Route::post('users/status', array('as'='user_status', 'uses'=>'Controllers\UsersController@postStatus');

Then, alter your post jQuery (which I think is the error's source)

$("a.delete").click(function () {
    var $id = $(this).attr("id");

    $.post('{{URL::route('user_status')}}',
        {
            id: $id
        }
    );

});

And your controller method:

public function postStatus()
{
    if(Request::ajax()) {
        $thisID = Input::get('id');
        DB::table('users')
                ->where('id', $thisID)
                ->update(array('activated' => 0));
    }
}

Didn't try, but should work.

I'd recommend making the changes suggested by @Arda, but I believe your jquery is incorrect. You are setting the key to be unique, and the value to be data .

Try this:

$("a.delete").click(function () {
    var id = $(this).attr("id");
    $.ajax({
      type: 'post',
      url: "{{URL::route('user_status')}}", 
      data: {'id' : id}
    });

This requires using a blade template, but that's pretty good practice anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM