How to debug ajax response?

2019-08-29 03:17发布

问题:

I am at lost with ways to debug my Ajax callback. I'm trying to print the response from the php file to see if the call is working but without success. While the call works, the response seem to return empty... Nothing shows in the console but the response in Network tab is 200 OK and I can see the data in the response header. However not able to print it back to the console. console.log(response) should print the table but meh. What are alternative ways to debug this? Any suggestion is welcome.

js

   $.ajax({
                    type: 'POST',
                    dataType : 'json',
                    url: 'queries/getsocial.php',
                    data:nvalues,
                    success: function(response)
                    {

                        console.log(response);//does not print in the console
                    }

            });

So here's the proof I get that it is working server side

回答1:

Can you add a photo of the console? When something like that happens to me, I usually print something console.log(“Response: “+ response). At least if you see the text at least you can be sure your function is getting executed.



回答2:

After a much needed 10 hour break into the night, I solved the issue with a much clearer mind. I removed the dataType row and it worked for me.

As silly as it may sound, the table in the PHP page was not in JSON format, so when jQuery tries to parse it as such, it fails.

Hope it helps for you. If not, check out more suggestions here.

Before

  $.ajax({
                    type: 'POST',
                    dataType : 'json',
                    url: 'queries/getsocial.php',
                    data:nvalues,
                    success: function(response)
                    {

                        console.log(response);//does not print in the console
                    }

            });

After (Works now)

  $.ajax({
                    type: 'POST',
                    url: 'queries/getsocial.php',
                    data:nvalues,
                    success: function(response)
                    {

                        console.log(response);//yes prints in the console
                    }

            });