Pass a view JSON to the route

Asked

Viewed 978 times

4

I’m trying to send a JSON of view for a route, but I’m not getting it. Unfortunately I know little about JSON, ajax, GET and POST.

In the view, the json:

var json = {
  "numeroMesa": numeroMesa,
  "itens": itens 
};

Remembering that the variable numeroMesa is a global variable in javascript and itens is an array with some numbers.

Soon after, I have:

$.get('addPedido/' + json, function(data) {
  alert(data);
});

And on the route, I have:

Route::get('/addPedido/{json}', function($json) {
  $json = JSON_decode($json);
  return $json;
});

When sending, Alert shows nothing. I’m using framework laravel.

1 answer

3


It’s not the best way, or I can still say it’s wrong.

You are concatenating a javascript object with a string (in the url).

  var json = {
      "numeroMesa": numeroMesa,
      "itens": itens 
  };

$.get('addPedido/' + json)

In turn Laravel, expects you to pass another segment (the URL word between bars /) when you use {json}.

A json string passed in the url would look bad and could cause errors. Not to mention you didn’t convert the javascript object to json through JSON.stringify.

The correct approach would be to use the parameters of url in the ajax request. Like this:

$.get('addPedido/', json, function () { /**...**/ })

So you could, by the route of Laravel, use the method Input::get to capture the values passed to the request parameter addPedido/

Thus:

Route::get('/addPedido', function() {
    $input = Input::only('numeroMesa', 'itens');

    var_dump($input['itens'], $input['numeroMesa']);
});

It is therefore not necessary to use the json to send the data to the Laravel. Just use the url parameters to capture them in a request GET.

  • Corrected. Not necessary {json} in that approach.

  • Thank you very much! Unfortunately when I make these changes you asked for, I have an internal error (500 Internal Server Error). And there are still some doubts, can you help me? 1) Since 'items' is an array, it can have multiple elements stored. Can I still run it through a regular get request? (so I was trying to use a json) 2) On the route, when I type var_dump, how do I see the result of this, since it is not being returned to the view through the 'Return'? Anyway, thank you so much for the reply.

  • @Gabrielaugusto can yes. The jQuery converts automatically. Take a test (to make you feel safer). Do this and see what appears on the console: console.log($.param(json)).

  • I found my mistake here, buddy, I was still spending $json on the route function. Thanks so much for the quick and direct help!

  • Just one more question: is there any way for me to verify what is this internal error that happens? For example, if I do something silly on the route, using PHP, and give an error, is it possible to see what this error is? thank you!

  • Yes. Go on config/app.php and configure the line "debug" => true.

  • This question here at SOPT would be important.

Show 2 more comments

Browser other questions tagged

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