AJAX request firing twice while success is called once

Viewed 2251

I am using laravel 5.4, my Ajax functionality is called twice every time due to which I create two users however I want to create one user per click

actually it includes another page and that page don't have its on

$(document).ready(function(){

or any function like

$(document).on('click','#addproject',function(){

but they includes script tags but if I don't include them then Ajax is called only once Ajax call:

$(document).on('click','#addproject',function(){

        $.ajax({
            type:'get',
            url:"{!! URL::to('addProject') !!}",
            data:{},
            success:function(data){
                console.log('success');
                console.log(data);
                console.log(data.length);
                $("#newproject").fadeIn();
            },
            error:function(){

            },
        });
    });

Routes:

Route::get('/addProject','HomeController@addProject');

Controller:

  public function addProject(Request $request){
        $project = new Project;
        $data = array(
        );

        $project->create($data);
    }
3 Answers

Try this,

$("#addproject").on('click',function(){
    //your code here
});

instead of,

$(document).on('click','#addproject',function(){
    //your code
});
Related