@@ -150,6 +150,16 @@ public function getServerParameter($key, $default = '')
150
150
return isset ($ this ->server [$ key ]) ? $ this ->server [$ key ] : $ default ;
151
151
}
152
152
153
+ public function switchToAjax ()
154
+ {
155
+ $ this ->setServerParameter ('HTTP_X-Requested-With ' , 'XMLHttpRequest ' );
156
+ }
157
+
158
+ public function removeAjax ()
159
+ {
160
+ unset($ this ->server ['HTTP_X-Requested-With ' ]);
161
+ }
162
+
153
163
/**
154
164
* Returns the History instance.
155
165
*
@@ -249,14 +259,15 @@ public function click(Link $link)
249
259
*
250
260
* @param Form $form A Form instance
251
261
* @param array $values An array of form field values
262
+ * @param bool $isAjax Whether it's ajax or not
252
263
*
253
264
* @return Crawler
254
265
*/
255
- public function submit (Form $ form , array $ values = array ())
266
+ public function submit (Form $ form , array $ values = array (), $ isAjax = false )
256
267
{
257
268
$ form ->setValues ($ values );
258
269
259
- return $ this ->request ($ form ->getMethod (), $ form ->getUri (), $ form ->getPhpValues (), $ form ->getPhpFiles ());
270
+ return $ this ->request ($ form ->getMethod (), $ form ->getUri (), $ form ->getPhpValues (), $ form ->getPhpFiles (), array (), null , true , $ isAjax );
260
271
}
261
272
262
273
/**
@@ -269,17 +280,21 @@ public function submit(Form $form, array $values = array())
269
280
* @param array $server The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does)
270
281
* @param string $content The raw body data
271
282
* @param bool $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload())
272
- *
283
+ * @param bool $isAjax Whether it's a ajax request or not
273
284
* @return Crawler
274
285
*/
275
- public function request ($ method , $ uri , array $ parameters = array (), array $ files = array (), array $ server = array (), $ content = null , $ changeHistory = true )
286
+ public function request ($ method , $ uri , array $ parameters = array (), array $ files = array (), array $ server = array (), $ content = null , $ changeHistory = true , $ isAjax = false )
276
287
{
277
288
if ($ this ->isMainRequest ) {
278
289
$ this ->redirectCount = 0 ;
279
290
} else {
280
291
++$ this ->redirectCount ;
281
292
}
282
293
294
+ if (true === $ isAjax ) {
295
+ $ this ->switchToAjax ();
296
+ }
297
+
283
298
$ uri = $ this ->getAbsoluteUri ($ uri );
284
299
285
300
$ server = array_merge ($ this ->server , $ server );
@@ -324,6 +339,10 @@ public function request($method, $uri, array $parameters = array(), array $files
324
339
$ this ->redirect = null ;
325
340
}
326
341
342
+ if (true === $ isAjax ) {
343
+ $ this ->removeAjax ();
344
+ }
345
+
327
346
if ($ this ->followRedirects && $ this ->redirect ) {
328
347
$ this ->redirects [serialize ($ this ->history ->current ())] = true ;
329
348
0 commit comments