How to debug AJAX (POST) with PhpStorm and Xdebug?

百般思念 提交于 2020-12-04 17:28:12

问题


I have configured PhpStorm to debug HTTP GET - but only when I load a page directly.

When I want to debug AJAX, I take the URL which my JS would request and create a PhpStorm configuration to debug it.

Not particularly elegant, is it?

And, of course, I can't do that for POST requests (or can I?).

Ideally, I would like to load my AngularJs app in the browser (Chrome) and be able to breakpoint and debug the backend in PhpStorm.

I googled a lot, and found much that came close, but I can't find the answer :-(

Who can help?


[Update] a few years later, and I am using the excellent and free Postman to test both GET and POST.


回答1:


If Xdebug and PHPStorm are configured to debug HTTP GET when loading a page normally, then simply include the GET parameter in the URL of the AJAX request in your Javascript. For example: http://example.com/script.php?XDEBUG_SESSION_START=PHPSTORM

Turn on Debug listening in PHPStorm, send the AJAX request with the new URL, and the debugger should catch it. The POST data you are looking for should appear in $_POST as usual.




回答2:


I am using kind of hacking method to debug AJAX requests. My project is Laravel. You can change this code as compatible with your technology.

Basic idea is:

  1. Grab home page debug port
  2. Create a session
  3. Using this session concatenate the AJAX url

When you start debugging, the port will be applied for all ajax urls which having + debug_url.

HomeController@index method

// Development purpose only
if ($request->has('XDEBUG_SESSION_START')) $request->session()->put('debug_port' , $request->get('XDEBUG_SESSION_START'));

master.blade.php

<script>
  var debug_url='?XDEBUG_SESSION_START={{session('debug_port')}}';
</script>

submit.blade.php

<script>
  $.ajax(url + debug_url, {
     method:'post',
     data:{}
  });
</script>


来源:https://stackoverflow.com/questions/38279815/how-to-debug-ajax-post-with-phpstorm-and-xdebug

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