【发布时间】:2015-02-07 11:13:37
【问题描述】:
我有一个从 Titan 客户端到 symfony2 服务器的 xhr 发布请求。这样做了很多次,但是这一次,无法得到任何我需要在代码中处理的post参数。
我的构造方法(或其中的一部分)如下
public function __construct() {
$this->request = Request::createFromGlobals();
}
所以,当我 var_dump $this->request->request->getContent();
我得到了输出
string(43) "id=9&token=d41d8cd98f00b204e9800998ecf8427e"
但我不知道为什么当我尝试访问 $this->request->get('token') 时我得到了 null
这些是初始化后的请求参数
object(Symfony\Component\HttpFoundation\ParameterBag)#83 (1) {
["parameters":protected]=>
array(0) {
}
}
以及请求头参数包
object(Symfony\Component\HttpFoundation\HeaderBag)#79 (2) {
["headers":protected]=>
array(12) {
["connection"]=>
array(1) {
[0]=>
string(5) "close"
}
["cache-control"]=>
array(1) {
[0]=>
string(13) "max-age=43200"
}
["host"]=>
array(1) {
[0]=>
string(19) "free.domain.com"
}
["user-agent"]=>
array(1) {
[0]=>
string(59) "Appcelerator Titanium/3.5.0 (iPhone/7.1; iPhone OS; it_IT;)"
}
["cookie"]=>
array(1) {
[0]=>
string(36) "PHPSESSID=saohjtso3767nohqfe0knd8ca6"
}
["accept-encoding"]=>
array(1) {
[0]=>
string(13) "gzip, deflate"
}
["accept-language"]=>
array(1) {
[0]=>
string(5) "it-it"
}
["content-length"]=>
array(1) {
[0]=>
string(2) "43"
}
["content-type"]=>
array(1) {
[0]=>
string(48) "application/x-www-form-urlencoded; charset=utf-8"
}
["accept"]=>
array(1) {
[0]=>
string(3) "*/*"
}
["x-requested-with"]=>
array(1) {
[0]=>
string(14) "XMLHttpRequest"
}
["x-titanium-id"]=>
array(1) {
[0]=>
string(36) "e46d43b1-9136-41b2-93da-6b1d5b3c4580"
}
}
["cacheControl":protected]=>
array(1) {
["max-age"]=>
string(5) "43200"
}
}
ps。 钛方面我发送我的json请求如下
xhr.open("POST", url,true);
xhr.send(data);
但是当我去的时候
xhr.send(JSON.stringify(data));
服务器端,我可以毫无问题地访问$this->request->get('token')
我的问题是我希望(总是)发送数据而不是 JSON.stringify(data),因为我还想发送 blob 文件(图像)。
pps。 以防万一,如果我不需要从手机发送图像,我可以这样安排我的服务器脚本:
$data = json_decode($this->request->getContent(), true);
$this->request->request->replace(is_array($data) ? $data : array());
但不适用于文件。
【问题讨论】:
标签: php ajax json symfony titanium