1
I have problems with the requisição JSON, she’s returning the html of the full page being that my request is of a JSON. I’m using framework Laravel in versão 5.2
I make my request like this:
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "http://localhost:8000/produtos/" . $id,
CURLOPT_HTTPHEADER => ["Accept" => "application/json"],
CURLOPT_RETURNTRANSFER => true]);
dd(curl_exec($curl));
In my controller that I get is like this:
if(Request::wantsJson())
{
$produtos = Produto::with('imagens')->where('FlgProdutoVisivel', '1')->get();
return compact('produtos');
}
else
{
$produtos = Produto::with('imagens')->get();
return view('Produto.listProduto', compact('produtos'));
}
I realized he’s falling in second, for some reason Request::wantsJson() is not working. I do this because this same função returns to view if you ask for the page normally and a JSON if I make a requisição JSON
Is falling into the
elseso? or it’s an error page, one can confirm?– novic
It’s falling on Isis, the full page comes
– Renan Rodrigues
the same page if I access directly from the link
– Renan Rodrigues
you are doing a GET or a POST?
– novic
The intention is to do a get, and I’m passing an id to this get
– Renan Rodrigues
Try Accept instead for Content-type
– novic
Sorry for the delay @Virgilionovic but it still didn’t work, it has to do with the controller, because if I do the request by jquery everything works, the problem that for this case I need to involve the controller, it has to be ?
– Renan Rodrigues
I believe you are not sending the correct header, check
$.ajax({
 type: "POST",
 contentType: "application/json",
 url: '/Hello',
 data: { name: 'norm' },
 dataType: "json"
 });in this example!– novic
That code is php, ajax not put, because it works
– Renan Rodrigues
put all related code, the ajax it would be important to understand why you are entering the
else! It may be working, but if you are not sending the header correctly you may fall intoelse! (how it is happening). It may also be that you have to changeRequest::wantsJson()forRequest::ajax()!– novic
@Virgilionovic Ajax is another part, this part I am in the controller and I make the request, there is another way to make the request ?
– Renan Rodrigues
Let’s go continue this discussion in chat.
– novic
we’re talking about different things...
– novic
Solved your doubt?
– durtto