Skip to content

Commit 8e28b7c

Browse files
committed
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.
1 parent 0ccbfcf commit 8e28b7c

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/Controller/Component/RequestHandlerComponent.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ public function initialize(Event $event) {
155155
if (isset($this->request->params['_ext'])) {
156156
$this->ext = $this->request->params['_ext'];
157157
}
158-
if (empty($this->ext) && $this->request->is('ajax')) {
159-
$this->ext = 'ajax';
160-
}
161158
if (empty($this->ext) || in_array($this->ext, array('html', 'htm'))) {
162159
$this->_setExtension();
163160
}
161+
if (empty($this->ext) && $this->request->is('ajax')) {
162+
$this->ext = 'ajax';
163+
}
164164

165165
$classMap = $this->_config['viewClassMap'];
166166
if ($classMap) {

tests/TestCase/Controller/Component/RequestHandlerComponentTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public function testInitializeContentTypeSettingExt() {
140140
*/
141141
public function testInitializeContentTypeWithjQueryAccept() {
142142
$_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, */*; q=0.01';
143+
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
143144
$event = new Event('Controller.initialize', $this->Controller);
144145
$this->assertNull($this->RequestHandler->ext);
145146
Router::parseExtensions('json', false);

0 commit comments

Comments
 (0)