2015-02-07 115 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)。希望這有助於某人