(Laravel) method $.get can’t find route

Asked

Viewed 661 times

1

Eae guys, all right?

I would really like your help because I’ve been stuck in this problem for a couple of days and I couldn’t figure it out on my own.

I have a multi-row table with a checkbox on each row and a button that, when clicked, takes all the lines that are selected, stores the ids of those lines in an array, transforms the array into a json and requests a route by passing this json, where it will be manipulated to go through all ids and make database changes.

Initially it worked smoothly, but for a while it stopped working, I don’t know why.

Javascript function (in the view):

    function tornarPendente() {

    var arrayIds = [];

    //pega todos os checkbox que estão selecionados e para cada um pega o ID referente ao aluno
    $.each($('.checkbox1').prop('checked', 'checked'), function() {
        var id = ($(this).attr('id'));
            arrayIds.push(id);  //coloca no vetor de ids
    });

    //converte o array em um json para passar para a rota
    var json = {
        "ids": arrayIds
    };

    $.get('../controle/concluidas/tornarTodasPendentes', json, function(data) {
        window.location.reload(true);
    });

}

Route

Route::get('/controle/concluidas/tornarTodasPendentes/', function() {
$input = Input::only('ids'); //recebe um array de ids
$input = $input['ids']; 
foreach($input as $id) {
    $aluno = new App\Aluno();
    $aluno = $aluno->where('id', '=', $id)->first();
    $aluno->pendente = '1';
    $aluno->save();
}
    return Redirect::back();    

});

NOTE: Strange is the fact that if I change the address of the route to a route that does not exist, javascript does not return any error (even if I remove the part of window.location.reaload(true) ).

OBS2: If I give an Alert(json['ids']) I can see the ids normally.

Is there any way I know if these ids have at least reached the route?

Any kind of help would be welcome.

Thank you very much!

  • Spoke, spoke, spoke but did not say which error returns.. rsrsr. Have you checked the obvious? For example, is the url correct in ajax? It is pointing the way in a relative way. It may be that it is pointing to a non-existent or wrong path.. Anyway.. To help debug use your browser’s "Developer tools" console. On Chrome, Ctrl+Shift+I

  • So, friend, as I said in Obs 1, javascript does not return any error (in the console of Developer tools), and even stranger is that it does not return me any error even when I change this url to a route that does not exist. That’s why I wanted to know if there’s any way I can verify if the data was actually passed to the route or not, through PHP. The routes are correct, including this function worked before any changes I made, but now they are no longer working... Anyway thanks for the help.

  • Reverse the change you made to the point where it worked then analyze what the change might have caused.

  • About not returning error, it was unclear what returns. That’s what I meant. If no error returns, what then? Blank page? A message? What is the http status? anyway.. hard to know without having the parameters..

  • To see debugging in the browser console, PHP has nothing to do with it. Open the browser tools (FF, Chrome, IE) and analyze the headers something like this. It usually stays in "Network". In Chrome it’s "Network" -> "Doc". Then you click on the document you want to analyze and see the logs. A tip, instead of using alert() to debug, use the console.debug(json['ids']), for example. Not that this will solve anything, but it is a better way to debug.

  • Another very grotesque way to test, go to the index.php of the Laravel and put a print_r($_GET); exit; well at the beginning, just to test. If receive then is good. The problem should not be javascript code. Then you already skip a step and do not waste time on javascript. It must be something that moved in the codes in the language as you mentioned yourself.

  • I tried to reverse the change, using git checkout for the old commit from six days ago inclusive, but it didn’t change anything. About the error, the page becomes static if I remove the line 'window.location.Reload(true)', but in network appears a new line as if OK, passing the ids, all right. Inclusive with a Status Code: OK (green). In Query String Parameters you pass all parameters normally (all ids).

  • Thanks for the tip to use the console.debug, I will do this in the future. About print_r($_GET); Exit; at the beginning of the index.php file, nothing changes when I click on the button that makes the GET request, the page looks static the way it was. This, of course, if I comment on these commands until I reach the desired page and uncomment only at the time of clicking the button, after selecting the rows of the table. Otherwise, if I leave the commands when I enter the page, I have only one blank page written Array(). Anyway, thank you!

  • Appears only as array()? then wouldn’t be getting as POST instead of GET? try print_r($_REQUEST); exit;. And instead of testing by ajax, play directly in the url bar of the browser. .enfim.. search where the problem is, get it? Sure, don’t forget to clean up the mess with these print_r and exit right..

Show 4 more comments

2 answers

4

Using the ../ can cause problems if you are on a route with a sub-segment.

For example,

localhost:8000/admin/.. /test/1

Is the same as

 localhost:8000/teste/1

In that case:

localhost:8000/admin/acao/../test

The result would be;

localhost:8000/admin/test

In the Laravel, for being dealing with an application that is always run at the "root" of your host, it is not necessary to use ../.

Maybe that’s what’s causing your problem.

  • That’s right the problem is in ../ ;)

  • I appreciate the answers, but I believe this is not yet the solution to my problem =( See: using ".. /control/completed/makeTodasPendentes" or without the two points, when I check on google’s google Core network, the request url is the same: "http://localhost/control/completed/makeToday Users? ids..." and get parameters. I’ve even tried to place a 'die('test')' on the route, but it’s not even executed. I also tried to create a function in a controller and use this controller on the route, but it doesn’t work.

  • In the 'Response' tab (within the network), after get, I only have the html / javascript code of the page I’m on

0

it was all a big mess of routes...

As I had said, it occurred after I made a new feature (which was a function to make an ascending and descending select), which used a route /control/pending/{ascDesc?} going to the student controller

This meant that any route in /control/pending/Whatever was for this controller, including the route I had created to make them all complete, since the route was: /control/pending/conclusionViews

Now I’ve changed course to

/control/concludingVarias, which causes it to fall into the correct controller...

Thank you so much for your help, for your patience...

Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.