這裏是我們的控制器:FosRestbundle不斷髮送text/html作爲響應,我們期待json。
function getLocationsAction(Request $request) {
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$query = $dm->createQueryBuilder('MainClassifiedBundle:Location')->select('name', 'state', 'country', 'coordinates');
$locations = $query->getQuery()->execute();
$data = array(
'success' => true,
'locations' => $locations,
'displaymessage' => $locations->count() . " Locations Found"
);
$view = View::create()->setStatusCode(200)->setData($data);
return $this->get('fos_rest.view_handler')->handle($view);
}
這裏是fosrestbundle的config.yml:
fos_rest:
view:
formats:
json: true
templating_formats:
html: true
force_redirects:
html: true
failed_validation: HTTP_BAD_REQUEST
default_engine: twig
這裏是路線:
MainClassifiedBundle_get_locations:
pattern: /locations/
defaults: { _controller: MainClassifiedBundle:ClassifiedCrudWebService:getLocations, _format:json}
requirements:
_method: GET
爲什麼我們得到的text/html? Ho wcan我們強制響應成爲application/json?
請幫助,因爲這是目前
既然有' - > setStatusCode()',有可能也是一種方法用於設置響應MIME類型。沒有手冊嗎? – mario
FosRestBundle默認應該做JSON,但是我的服務器一直在發送文本/ html – jini
你如何測試它? –