Php 未在标头的内容类型中获取appilation/json
我想发送数据并以JSON的形式获得响应,以实现快速响应。所以我使用的是AJAX。 为什么我会 “内容类型text/html;字符集=UTF-8” 为什么不 内容类型应用程序/json 控制器Php 未在标头的内容类型中获取appilation/json,php,json,jquery,cakephp,Php,Json,Jquery,Cakephp,我想发送数据并以JSON的形式获得响应,以实现快速响应。所以我使用的是AJAX。 为什么我会 “内容类型text/html;字符集=UTF-8” 为什么不 内容类型应用程序/json 控制器 public function testingMethod() { $this->autoRender = false; $urlVal = $_POST['urlVal']; $dataBack = json_encode($urlVal);
public function testingMethod() {
$this->autoRender = false;
$urlVal = $_POST['urlVal'];
$dataBack = json_encode($urlVal);
if ($this->RequestHandler->isAjax()) {
return $dataBack;
}
}
Response Headers
Connection Keep-Alive
Content-Length 2382
Content-Type text/html; charset=UTF-8 //why here not getting application/json
Date Wed, 14 Aug 2013 10:17:38 GMT
Keep-Alive timeout=5, max=94
Server Apache/2.2.22 (Win32) PHP/5.4.3
X-Powered-By PHP/5.4.3
jQuery
$.ajax({
type: "POST",
url: pathname+"Frontends/testingMethod",
data: 'urlVal=' + urlVal,
dataType: 'json',
success: function (result) {
console.log(result);
alert(result);
}
});
标题
public function testingMethod() {
$this->autoRender = false;
$urlVal = $_POST['urlVal'];
$dataBack = json_encode($urlVal);
if ($this->RequestHandler->isAjax()) {
return $dataBack;
}
}
Response Headers
Connection Keep-Alive
Content-Length 2382
Content-Type text/html; charset=UTF-8 //why here not getting application/json
Date Wed, 14 Aug 2013 10:17:38 GMT
Keep-Alive timeout=5, max=94
Server Apache/2.2.22 (Win32) PHP/5.4.3
X-Powered-By PHP/5.4.3
快速检查表:
.json
扩展添加到routes.php
:
Router::parseExtensions('json');
RequestHandler
类:
public $components = array(
'RequestHandler',
);
View
目录中创建一个json
文件夹,并将json视图放在那里.json
附加到URL:
url: pathname+"Frontends/testingMethod.json",
这记录在。阅读完整的文档是值得的,因为我描述的步骤并不是唯一可行的方法。我不熟悉JQuery,但在执行POST时,几乎肯定需要设置“Content Type”标题值(到“application/json”)。否则它可能只是默认值。这就是为什么您实际上没有发送
内容类型
标题。