Route does not execute controller method

1

Good afternoon. I have a problem with my php code developed in Laravel 4. I have a function in JavaScript that performs an action using $ .post and on success, calls another Javascript function, this second using $ .ajax. The two in .blade pages. The problem is that when entering the second function, I suspect that the route passed in the url field is not found, but I can not find the error. Can someone help me? Here are the codes:

Javascript functions:

function form_variations_submit()
{
    $.post(
        '{{route('admin.ajax.save')}}',
        $('#form').serialize(),
        function(data) {
            if(data.sucesso) {
                alert("Alterações Salvas! "+data.pvid);
                var pvid = data.pvid;
                alert("pvid = "+pvid);
                var flagOrder = envia_pvid(pvid);
                alert("FlagOrder = "+flagOrder);
            } else {
                alert(data.erro);
            }
        }
    );
    return false;
}

function envia_pvid(id){
    var pId = id;
    alert("Entrou na 2 função! Envia_id "+ pId);
    $.ajax({
        type: 'POST',
        url: '{{route('admin.ordem')}}',
        data: { pId : pId
        },
        sucess: function(data) {
            if(data.pvid){
                var ret = data.pvid;
                alert(ret);
                return ret;
            } else{
                alert("ERRO!");
            }
        },
        dataType: "json"
    });
}

Route:

Route::post('p_var', array('as' => 'admin.ordem', 'uses' => 'Admin_Controller@setaOrdem'));

Function OrderOrder in Controller:

public function setaOrdem()
{
    $pvid = Input::get('pId');

    return Response::json(array('sucesso' => true,'pvid' => $pvid));
}

Alerts are run nominally until the FlagOrder alert returns "undefined".

    
asked by anonymous 22.12.2015 / 19:01

2 answers

0

I'll give you some tips that can help you solve the problem:

First I would like to highlight about Javascript .

What differs from the $.post of jQuery referring to $.ajax is that $ .post does not have the error key to do error treatments, since $ .ajax has. So if one function depends on the other in case of success, I advise you to use $ .ajax in the two functions to catch the errors:

$.ajax({
  url: '//sua url',
  type: 'tipo da requisição',
  data: //dados de envio,
  success: function(response)
 {
    //retorno
 },
 error: function(xhr)
 {
   console.log(xhr.responseText);
 }
});

This is important because certain types of errors are only possible to catch through the error function and many people do not know the differences between $.post , $.get and $.ajax . But that was just a tip, this situation does not interfere with the location of the route.

Now about Laravel

I noticed that you are using named routes. As you did not leave the real url to illustrate the possible errors:

Creating routes in laravel

When you use routes with request POST

Route::post('url','Controller@metodo');

For GET

Route::get('url/{identificador}','Controller@metodo');

For any request

Route::any('url','Controller@metodo');

If you are in localhost and have not created the virtual host you have to specify the full url in javascript for the route to be found. Already in production you do not have this problem if the pointing to the public folder is configured. If you created the virtual host it is important that you pass the URL as follows:

$.ajax({
    url : '/url real não nomeada' //EX: '/admin/ajax-savar-dados'
});

I do not advise you to use the named route in javascript. Firstly because it is a bad practice to put PHP in the middle of javascript. It may even be one of the reasons for mistakes. Ideally you put the javascript in a separate file.

To test your request $ .ajax use the browser console in the

network > xhr

    
05.05.2016 / 15:09
0

If you look at the network tab in the developer tools of your browser you will probably notice the 404 error.

As far as I understand the public folder of your project is not the main folder, Laravel to work correctly needs the public folder to be root of the server (for example apache), otherwise you will have to declare the subfolders and this will be difficult when moving to production.

Laravel works exactly like this, oriented to the folder public of your project, like it being the main thing of everything, if your project is in a folder like this:

  • /etc/www/projeto1

And you try to access:

  • http://localhost/projeto1/ or http://localhost/projeto1/public/

The routes will not understand the path, since they work from the first / after localhost in url http://localhost/

Apache:

If it's Apache you can change the documentroot:

DocumentRoot "/home/user/projeto-em-laravel/public"
<Directory "/home/user/projeto-em-laravel/public">
    AllowOverride all
</Directory>

Ngnix:

In Nginx you use root to point:

root /home/user/projeto-em-laravel/public/;

These examples are for development, for production you will probably have to edit virtualhost, or do that even in this answer:

22.12.2015 / 19:29