How to debug ajax response?

妖精的绣舞 提交于 2019-12-24 23:57:50

问题


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
                    }

            });


来源:https://stackoverflow.com/questions/50939126/how-to-debug-ajax-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!