JSON request comes full page

Asked

Viewed 121 times

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 else so? or it’s an error page, one can confirm?

  • It’s falling on Isis, the full page comes

  • the same page if I access directly from the link

  • you are doing a GET or a POST?

  • The intention is to do a get, and I’m passing an id to this get

  • Try Accept instead for Content-type

  • 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 ?

  • 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!

  • That code is php, ajax not put, because it works

  • 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 into else! (how it is happening). It may also be that you have to change Request::wantsJson() for Request::ajax()!

  • @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 ?

  • we’re talking about different things...

  • Solved your doubt?

Show 9 more comments
No answers

Browser other questions tagged

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