Vivek Choudhary
Vivek Choudhary

Reputation: 684

Laravel ajax URL issue

I'm new to Laravel and using Ajax for some functionalities.

//Route
Route::post('some/thing/','Controller@Method');

//jQuery
$('.some_class').click(function(){
        var var1 = $('.some').val();
        var val2 = $(".another").val();
        var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
        $.ajax({
            //this part
            url: "some/thing/",
            type:"POST",
            data: { var1: var1,val2: val2,_token: CSRF_TOKEN},
            success:function(response){
               console.log("working");
            },
            error:function(){
                console.log("error");
            }
        });
    });

//controller
public function Method(Request $object){
   if(isset($_POST['val1'])){//do something}
}

problem is in the URL parameter of AJAX. When I'm giving value to the url i.e some/thing/, it gives me 404 error showing www.siteurl/some/thing/some/thing/ not found and when I'm keeping url value blank then it's working. But then i don't think it's a good practice to do like this.

I have seperate .js file in public folder. Controller in different and blade file in different directory. Laravel version 5.6.22

thank you.

Upvotes: 4

Views: 6249

Answers (4)

Narinder Bajwa
Narinder Bajwa

Reputation: 54

You can add route in $except array in VerifyCsrfToken middle ware to ignore csrf token verification on that route

 namespace App\Http\Middleware;

    use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

    class VerifyCsrfToken extends BaseVerifier
    {
        /**
         * The URIs that should be excluded from CSRF verification.
         *
         * @var array
         */
        protected $except = [

              "/some/thing/"

        ];
    }

Upvotes: 0

Roland Allla
Roland Allla

Reputation: 396

I think you have to change the Url to the absolute path:

Incase you are working on Blade file:

Change the url from : url: "some/thing/",

To url: {{url('some/thing')}},

In case you are working on external Js File:

Change the url from : url: "some/thing/",

To url: url: "/some/thing/",

Upvotes: 3

FULL STACK DEV
FULL STACK DEV

Reputation: 15951

Use absolute path instead of relative. append / before the url like "/some/thing/"

$('.some_class').click(function(){
        var var1 = $('.some').val();
        var val2 = $(".another").val();
        var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
        $.ajax({
            //this part
            url: "/some/thing/",
            type:"POST",
            data: { var1: var1,val2: val2,_token: CSRF_TOKEN},
            success:function(response){
               console.log("working");
            },
            error:function(){
                console.log("error");
            }
        });
    });

Hope this helps.

Upvotes: 2

Erkan Özkök
Erkan Özkök

Reputation: 891

When you write the url to ajax its trying to achieve some/thing/some/thing/ To fix; give a name for your route and then use this name for your ajax url.

//Route
Route::post('some/thing/','Controller@Method')->name('yourRouteName');

//jQuery
$('.some_class').click(function(){
        var var1 = $('.some').val();
        var val2 = $(".another").val();
        var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
        $.ajax({
            //this part
            url: "{{ route('yourRouteName') }}",
            type:"POST",
            data: { var1: var1,val2: val2,_token: CSRF_TOKEN},
            success:function(response){
               console.log("working");
            },
            error:function(){
                console.log("error");
            }
        });
    });

Upvotes: 2

Related Questions