0

So I'm trying to understand routing in laravel, without much success I'm afraid. My current issue is that I am trying to route so that my login function, which is checked using ajax, is accessible from all pages on the site. At the moment i can only reach it from my index. i use the following

Route::post('login', array('uses' => 'LoginController@doLogin'));

and the following ajax:

    $.ajax({
        type: "POST",
        url: "<%= ResolveUrl("~/login") %>",
        data: "username="+username+"&password="+password,
        success: function(data) {
            console.log(data)
            if(data == 'Fel användarnamn eller lösenord.') {
                document.getElementById('loginerror').innerHTML = data
            } else if (data == 'Inloggad'){
                document.getElementById('loginerror').innerHTML = data;
                window.location.reload(true);
            }

Now it is to my understanding that resolving the URL would possibly solve my issue. However, no matter i manipulate the url part, I cant get the resolve to work. I only get syntaxerrors or bad request errors. If there's something built in into laravel for this, to get the correct route independently of changes in the URL, I would greatly appreciate any tip of such functionality. I cant find/understand anything in the documentation that would solve my problem. Else if anyone could tell me how to write the ResoleUrl correctly, that would also be greatly appreciated. however an understanding of laravels routing would probably serve me better! Thanks beforehand!

2
  • Is this AJAX inside a .blade.php file? Commented Apr 17, 2014 at 13:20
  • the ajax is inside a javscript file, linked into a .blade file. Commented Apr 17, 2014 at 14:11

3 Answers 3

4

Change the url in your ajax to:

url:"/login"
Sign up to request clarification or add additional context in comments.

2 Comments

wow.. Well, I thank you for that! kind of exaclty what i was looking for! however, it still doesn't work, I get a the following instead: POST localhost/login 404 (Not Found). do I need to change something in the route itself aswell?
@user3084471 didn't realise you were on localhost. Assuming your working in a subfolder such as localhost/myapp ? If so then you can either /myapp/login (it will work, but you will have to change it if you change the subfolder or you go live to a real domain with no subfolder). The other alternative is to set the domain in laravel app/config/app.php to your "base url" such as localhost/myapp then use the built in url resolver URL::to('login')
0

Try this in your route.

Route::any('login', ['uses' => 'LoginController@doLogin', 'as' => 'login']);

1 Comment

Thank you for the tip. unfortunatley it didn't work. get the following error: POST localhost/login 404 (Not Found)
0

Let Laravel do it for you.

Name your route...

Route::post('login', array('uses' => 'LoginController@doLogin', 'as' => 'ajax.doLogin'));

Link to your route by the name in Javascript...

url: "{{ URL::route('ajax.doLogin') }}",

2 Comments

Can you do that in an external javascript file? Will blade be resolved?
No but you can put it into a new .blade.php file in the views folder and include it via blade @include('yourfile')

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.