Browse Source

Fix incorrect type detection when ajax + json are available.

Make ajax happen after content-type negotiation so that when jQuery
talks to CakePHP the requests don't get marked as ajax and instead get
marked as json.
mark_story 11 years ago
parent
commit
8e28b7c277

+ 3 - 3
src/Controller/Component/RequestHandlerComponent.php

@@ -155,12 +155,12 @@ class RequestHandlerComponent extends Component {
 		if (isset($this->request->params['_ext'])) {
 			$this->ext = $this->request->params['_ext'];
 		}
-		if (empty($this->ext) && $this->request->is('ajax')) {
-			$this->ext = 'ajax';
-		}
 		if (empty($this->ext) || in_array($this->ext, array('html', 'htm'))) {
 			$this->_setExtension();
 		}
+		if (empty($this->ext) && $this->request->is('ajax')) {
+			$this->ext = 'ajax';
+		}
 
 		$classMap = $this->_config['viewClassMap'];
 		if ($classMap) {

+ 1 - 0
tests/TestCase/Controller/Component/RequestHandlerComponentTest.php

@@ -140,6 +140,7 @@ class RequestHandlerComponentTest extends TestCase {
  */
 	public function testInitializeContentTypeWithjQueryAccept() {
 		$_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, */*; q=0.01';
+		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
 		$event = new Event('Controller.initialize', $this->Controller);
 		$this->assertNull($this->RequestHandler->ext);
 		Router::parseExtensions('json', false);