Sending data to Controller via Ajax Laravel

Asked

Viewed 1,284 times

0

I have a project in which I have to send a patient’s CPF to a method in the controller in order to do a search within this method and already return the value of the search to the view , however I am not able to send these values to the view.

Updating I am already able to access the controller and return the object to the ajax but I am not yet able to access the ajax. no error appears and I can verify on the network that the controller method is returning the data correctly.
follows new updated code:

controller

public function verificarCadastro (Request $request){

     return \Response::json($this->paciente->where('cpf',$request->cpf)->get());        

}

ajax in view

    function buscarCpf() {

    cpf = $('#cpfBusca').val();



    $.ajax({

        url: window.location.href+"/verificar-cadastro", 
        type: "POST",
        data: {"cpf": cpf,"_token":"{{csrf_token()}}" },
        cache: false,
        processData: true,
        dateType:'json',
        sucess: function(data) {

            if(data.cpf != null)
                console.log(data.cpf);

        },
        erro: function(data){

            console.log(data);
        }       

    });
              //window.location.href = caminho;


     };

return that is coming on the network

inserir a descrição da imagem aqui

1 answer

1

PHP is back-end, it processes and generates pages, whether HTML, txt, image, etc...

The variable data is Javascript and in turn is processed on the front end, well after the HTML page has been generated by PHP+Laravel, you will not be able to play the variable data in something that "has already been processed"!:

"{{ URL::route('hospedagem.registrar',data) }}";

That is, there is no way to pass a Javascript variable to PHP directly, as I explained in:

  • how can I be solving this case then?

  • @Márciocésar you can use Session in the back-end (in Laravel) or querystring in window.location, but it will depend on what your routes do, there’s no way to give a precise answer because I have no idea what your code is.

  • edited the code I’d like you to take a look at

  • @Márciocésar you only changed the Javascript part and posted only part of the controller, in this case the verificarCadastro, the way this I can’t even know where to start, I need to understand where you want to send JSON data. Understand?

  • then I am making a check to know if the registration exists in the case if it returns the json in sucess: from ajax and I can at least run the console.log() on my screen with the data you are asking would already cure my doubt, however it does not return anything there in sucess and not in error, but the data is being sent by the controller as you can check on the network I put in the image

  • @Márciocésar I understand, but the window.location? Please read this link https://answall.com/help/mcve. and try to revise the question, because you have changed the context of the question and still have not explained well what you have done, I really want to help, but there is no way to understand it. I recommend that you follow the recommendations and I will surely be able to help you.

  • I was able to solve the problem by making a simple correction in the code the name sucess was wrong : the correct is Success.

Show 2 more comments

Browser other questions tagged

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