我在前端使用基於ZF2的應用程序和Backbone時遇到問題。某處在frontent我跑PHP ZF2 - Restful控制器Backbone PUT方法不正確的解析
this.model.save({
city_id: parseInt(this.$el.find('#city_id').val()),
from: this.$el.find('#from').val(),
to: this.$el.find('#to').val(),
price: parseInt(this.$el.find('#price').val())
});
我打開我的瀏覽器嗅探器,看看該請求的詳細信息:
PUT /account/trip/2 HTTP/1.1
Host: jamydays.ru
Connection: keep-alive
Content-Length: 186
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://jamydays.ru
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31
Content-Type: application/json
Referer: http://jamydays.ru/account
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: PHPSESSID=pekjbefmi1jn01q5fgm4gu6jk0; _ym_visorc=w
,並要求有效載荷是:
{"from_formatted":"10 маÑ","to_formatted":"19 маÑ","url":"/account/trip","id":2,"city_id":65170,"city":"Baardheere","from":"10-05-2013","to":"19-05-2013","price":500,"is_active":1}
電腦板用來處理此請求運行適當的動作:
class TripController extends AbstractRestfulController{
...
public function update($id, $data){ var_dump($id, $data);exit(); }
...
}
我的問題是,我在這個結果看:
string(1) "2"
array(1) {
["{"from_formatted":"10_мая","to_formatted":"19_мая","url":"/account/trip","id":2,"city_id":65170,"city":"Baardheere","from":"10-05-2013","to":"19-05-2013","price":500,"is_active":1}"]=>
string(0) ""
}
在這裏,我們看到,ID解析不錯,但所有數據落入一些怪陣的關鍵。現在我正在從這個關鍵字中檢索數據,但是猜測這是不好的方法。任何人都可以幫助我弄清楚如何使控制器解析數據合適。
UPDATE
那麼它似乎解決方案只是更新ZF2至2.2的穩定版本。
你正在使用的zf2的版本是多少(Afaik至少需要2.1)?在類TripController中嘗試'protected $ jsonDecodeType = Json :: TYPE_OBJECT;'。 – jgb 2013-05-22 20:33:55
@jgb更新到ZF2.2穩定幫助。 – zeliboba 2013-05-24 09:27:00
你能把它標記爲已解決嗎? – ThaDafinser 2015-04-02 08:15:02