简体   繁体   English

laravel ajax发布问题

[英]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. 我可以使用get而不是post使其工作。 Im using Laravel 4 and jquery. 我正在使用Laravel 4和jquery。

My JS looks like this: 我的JS看起来像这样:

$('.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: 在该页面上,我设置了一个带有用户ID的按钮,然后单击该按钮,我调用了另一个JS代码段:

$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. 模态加载很好,它可以找到状态页,但是控制器中实际上什么也没发生:(我在那儿用硬编码2对其进行了测试。

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: 首先,使您为URL命名的路由更具动态性:

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) 然后,更改您的jQuery帖子(我认为这是错误的来源)

$("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. 我建议进行@Arda建议的更改,但我相信您的jquery不正确。 You are setting the key to be unique, and the value to be data . 您将键设置为唯一,值设置为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. 这需要使用刀片模板,但这还是一个很好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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