Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX request firing twice while success is called once

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);
    }
like image 778
Junaid Masood Avatar asked Oct 18 '22 05:10

Junaid Masood


1 Answers

I solved it, it turns out to be I was loading this page using load(), but I should have loaded this page using window.location.href = "updateform"; Very basic mistake though. and solution might was my app specific.

like image 171
Junaid Masood Avatar answered Oct 20 '22 23:10

Junaid Masood