I have updated the version of Laravel 5.2 to 5.6 and I am having problems with the requests via ajax that receives a return Sponse()->json

Asked

Viewed 41 times

0

I have a system running normally in Laravel 5.2, but I need resources that only have the most current versions of the framework. With this I updated it to version 5.6 and I am having problems with ajax requests that previously works normally in Aravel 5.2.

Updated script with tips from contributors who answered this question .

<script>

        jQuery("form.form").submit(function () {
            var dados = jQuery(this).serialize();
            $.ajax({
                type:'POST',
                url: jQuery("form.form").attr("action"),
                data: dados,
                success:function(data){
                    alert('Sucesso');
                },
                error:function(jqxhr, textStatus, err){
                  console.log(err);
                },
            });
            return false;
        });

</script> 

controller

public function Buscar()
{
    //caso eu retorno apenas uma string como abaixo, funciona normalmente e caí no 
    //método success
    return 'teste';

    //caso eu retorno um array json que é o que preciso  ele caí no método 
    //error com a seguinte mensagem SyntaxError: Unexpected token
    //in JSON at position 0
    //at parse (<anonymous>)


    return  response()->json(['pagina'=>'teste','result'=>'1']);
}

Note: In both cases when monitoring in the browser console the data is returned right in the 'network tab'

Conclusion, if I return a string works normal, but if an array returns it enters the error with the message Syntaxerror: Unexpected token in JSON at position 0 parsat() in the console, but still it gives the return of data in the network tab.

  • Already tried to define a dataType:"json" ??

  • If when tracking through the console the data is coming, the problem would not be at the time of manipulating this data?

  • When you return Return Response()->json(['page'=>'test','result'=>'1']); it appears in the console in the network tab?

  • yes, it appears correctly in the network tab, but falls in the script error method, I have also tested dataType:"json" .

  • 1

    I’d rather identify the mistake if you did: error:function(jqxhr, textStatus, err){ console.log(err); },

  • Valdeir, following his tip got the most detailed error, now I have this return Syntaxerror: Unexpected token in JSON at position 0 at parse (<Anonymous>) , then I tried to add the token in the returning array: Return Response()->json(['token'=>$token,'pagina'=>$p,'result'=>'1']); this $token variable is the value of the _token field that is created by the Laravel , picked it correctly in the controller and added it in the array at the position promiera, but even adding the token it continues to give the error

  • Try this in the header <meta name="csrf-token" content="{{ csrf_token() }}" /> And this in the script <script type="text/javascript"> $. ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); </script>

  • tried this tip,keeps giving the error Syntaxerror: Unexpected token in JSON at position 0 at parse , literally translating it as an unexpected Token in JSON at position 0

Show 3 more comments
No answers

Browser other questions tagged

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