4

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.

3
  • 1
    Shouldn't you use ajaxSetup headers? laravel.com/docs/5.6/csrf#csrf-x-csrf-token Commented Jun 21, 2018 at 8:28
  • Try by giving CSRF token seperately Commented Jun 21, 2018 at 8:29
  • Like this headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}, Commented Jun 21, 2018 at 8:29

4 Answers 4

3

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/",

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

If unable to use a route name, this solution works! Thank you!
2

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");
            }
        });
    });

4 Comments

didn't work for me. May be because I'm using different .js file for ajax. it made a URL like www.siteurl/some/thing/some/thing/20%{{ route('yourRouteName') }}20%. Thank you anyways.
{{ route('yourRouteName') }} should be in php file. it is php code.
and if you dont want to put your js to php file, try this; url: "/some/thing/"
yes sir, used the second solution and now it's working perfectly. Thanks for the help.
0

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/"

        ];
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.