Wednesday, March 1, 2017

How to abort laravel(php) execution from javascript (ajax)?

I have js code :

$('#button').on('click', function(e){
    console.log('start');

    if(xhr != ''){
        console.log('terminate');
        xhr.abort()
    }else{
        console.log('NOT terminate');
    }

    xhr = $.ajax({
        url: url,
        method: method,
        success: function(json) {
            console.log(json);
        },
    });
    console.log('end');
    return false;
});

and server laravel code:

public function update(Request $request)
{
    DB::transaction(function () {
       sleep(10);
       Product::find(1)->increment('user_id');
    });
    return 'test';
}

when I double click button output is :

start
NOT terminate
end
start
terminate
end
test

user_id is initially 1, but after execution it is 3 but I want to be 2. So how to terminate(abort) laravel execution from javascript ?




via fico7489

Advertisement