2015-02-07 112 views
0

我有一个来自titanium客户端的xhr发布请求到symfony2服务器。做了很多次,但是这个,不能得到我需要在代码中处理的任何post参数。symfony 2 json来自titanium xhr的请求

我的构造方法(或它的块)是遵循

public function __construct() { 
    $this->request = Request::createFromGlobals(); 
} 

因此,在任何时候在我的控制器的动作时,我var_dump$this->request->request->getContent(); 我得到的输出

string(43) "id=9&token=d41d8cd98f00b204e9800998ecf8427e" 

,但我不知道为什么当我试图访问$this->request->get('token')时我得到了空

这些是启动后的请求参数alization

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()); 

,但不处理文件。

回答

0

我错了,钛客户xhr发送的图像不起作用的事实。 INFACT留下

$data = json_decode($this->request->getContent(), true); 
    $this->request->request->replace(is_array($data) ? $data : array()); 

Symfony2的一面,会让我得到$this->request->file('image')但是当我送钛身边,我没有JSON.stringify(data)。希望这有助于某人