diff --git a/components/browser_kit.rst b/components/browser_kit.rst index bbfdd274034..23f3daa8c99 100644 --- a/components/browser_kit.rst +++ b/components/browser_kit.rst @@ -75,6 +75,19 @@ The value returned by the ``request()`` method is an instance of the :doc:`DomCrawler component `, which allows accessing and traversing HTML elements programmatically. +The :method:`Symfony\\Component\\BrowserKit\\Client::xmlHttpRequest` method, +which defines the same arguments as the ``request()`` method, is a shortcut to +make AJAX requests:: + + use Acme\Client; + + $client = new Client(); + // the required HTTP_X_REQUESTED_WITH header is added automatically + $crawler = $client->xmlHttpRequest('GET', '/'); + +.. versionadded:: 4.1 + The ``xmlHttpRequest()`` method was introduced in Symfony 4.1. + Clicking Links ~~~~~~~~~~~~~~ diff --git a/testing.rst b/testing.rst index 9f0daeee635..39e30902245 100644 --- a/testing.rst +++ b/testing.rst @@ -359,8 +359,8 @@ returns a ``Crawler`` instance. ) The ``server`` array is the raw values that you'd expect to normally - find in the PHP `$_SERVER`_ superglobal. For example, to set the ``Content-Type``, - ``Referer`` and ``X-Requested-With`` HTTP headers, you'd pass the following (mind + find in the PHP `$_SERVER`_ superglobal. For example, to set the + ``Content-Type`` and ``Referer`` HTTP headers, you'd pass the following (mind the ``HTTP_`` prefix for non standard headers):: $client->request( @@ -369,9 +369,8 @@ returns a ``Crawler`` instance. array(), array(), array( - 'CONTENT_TYPE' => 'application/json', - 'HTTP_REFERER' => '/foo/bar', - 'HTTP_X-Requested-With' => 'XMLHttpRequest', + 'CONTENT_TYPE' => 'application/json', + 'HTTP_REFERER' => '/foo/bar', ) ); @@ -441,6 +440,19 @@ script:: $client->insulate(); +AJAX Requests +~~~~~~~~~~~~~ + +The Client provides a :method:`Symfony\\Component\\BrowserKit\\Client::xmlHttpRequest` +method, which has the same arguments as the ``request()`` method, and it's a +shortcut to make AJAX requests:: + + // the required HTTP_X_REQUESTED_WITH header is added automatically + $client->xmlHttpRequest('POST', '/submit', array('name' => 'Fabien')); + +.. versionadded:: 4.1 + The ``xmlHttpRequest()`` method was introduced in Symfony 4.1. + Browsing ~~~~~~~~