Laravel vs. Ajax: incorrect routing

I am trying to populate a "position" dropdown based on "department". The Ajax call is triggered on change

the "department" dropdown event.

The problem is that the Ajax call cannot reach the correct route:

  • c url: 'ajax/get-position'

    - url: localhost / public / join / ajax / get-position? dept_id = 5
  • c url: '/ajax/get-position'

    - url: localhost / ajax / get-position? dept_id = 5

Both urls are wrong and I have no idea why. Especially that / join / in the first point is a mystery to me. The correct url should be localhost / public / ajax / get-position? Dept_id = 5 . I believe there is some sort of routing conflict, however I'm not sure where.

The Ajax call is made on the localhost / public / join / editor page .

JS:

...
...
$.ajax({
    url: 'ajax/get-position',
    data: {
        dept_id: value
    },
    type: 'GET',
    dataType : 'json',
    success: function(json) {
        //
    },
    error: function(xhr, status, errorThrown) {
        //
    }
});
...
...

      

Routes

Route::get('join/editor',
    array(
        'uses' => 'DefaultController@showEditorRegistration',
        'as' => 'editorRegistration'
    )
);

Route::post('join/editor',
    array(
        'uses' => 'DefaultController@createEditor',
        'as' => 'createEditor'
    )
);

// ROUTE FOR AJAX CALL
Route::get('ajax/get-position',
    array(
        'uses' => 'DefaultController@getPositionsByDepartment',
    )
);

      

Any ideas?

EDIT:

JavaScript is in an external file. If I put JS directly in view.php and use it URL::route('routename')

as Ajax value url

everything works fine. However, just using url: ajax/get-position

is not. Crazy world.

+3


source to share


1 answer


I'm not entirely sure, but based on our conversation about the details of your project, I think the problem is with the location of your document root. the directory public

should never appear in any of your laravel project urls.

I've created a demo project to demonstrate simple interaction with Laravel and Ajax . This is a vanilla laravel project with minor changes in presentation hello

.

Checkout the project, go to the shared folder and use the following command to deploy ad-hoc phperver:

php -S localhost:8002

      

Then you can go to the URL http://localhost:8002

to go to the home page.

If you check the link on the master page and look at the url that is generated with the facade URL

for the route tester

. The url does not include the directory public

:

http://localhost:8003/tester

      



You can also look at the ajax settings and see what route you can use tester

.

    $('#getbutton').click( function (){
            $.ajax({
                url: 'tester'

                }).complete(function (a){
                    alert(a.responseText);
                }).error(function (a){
                   console.log(a);
                });
    });

      

Using link or ajax call on button click click route tester

in routes file:

Route::get('tester',[ 'as' => 'tester', 'uses' => function (){
    return 'worked';
}]);

      

The link can get into the route via the route name tester

assigned in the routes file and the ajax request can get into the route from the request string.

The routes in your project looks ok and using ajax/get-position

as your url in the ajax call shouldn't be a problem.

Try this project. Even if the problem is not a web route, hopefully this helps you understand where your problem is coming from.

+2


source







All Articles