私はサーバー側とクライアント側の間に問題があります。私はPHP Symfonyでサーバー側にRest APIを持っています。 サーバー側:私は、適切なJSONオブジェクトを得たapplication/x-www-form-urlencoded
:PHP Symfony APIとjQuery Ajaxリクエスト
/**
* @Route("/advertisement/all", name="advertisement_get_all")
* @Method("POST")
*/
public function getAllAdvertisement()
{
header('Content-Type: application/json');
if ($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
$content = $this->get("request")->getContent();
$advertisemenetService = $this->container->get("advertisementservices");
$response = $advertisemenetService->getAllAdvertisement($content);
} else {
$response = new \stdClass();
$response->error = true;
$response->message = "Error occurred: You aren't authorized!";
}
return new JsonResponse($response);
}
私はコンテンツの種類とその development.domain.com/index.php/api/v2/advertisement/all
ためDHC休憩クライアントクローム拡張でそれをしよう。私はapplication/json
symfonyで同じことをしようと私のために、次のエラーを言う: Failed to start the session because headers have already been sent by "" at line 0. (500 Internal Server Error)
JSON response example
クライアント側:
私はAPIのテスターに言っどう私は、適切なJSONオブジェクトを得ました。私のクライアント側コード:
function sendAjaxRequest(method, url, data, contentType) {
var response;
$.ajax({
method: method,
url: url,
data: data,
dataType: 'json',
contentType: contentType,
success: function(msg) {
response = msg;
}
});
return jQuery.parseJSON(response);
}
response = sendAjaxRequest("POST", "{{ path('advertisement_get_all') }}", '', 'application/x-www-form-urlencoded');
document.getElementById("loader-container").innerHTML = response;
この場合、私は常にクライアント側にundefined
を取得します。 JSONオブジェクトであるため、レスポンスに対してJSON.stringifyを使用しようとしています。
あなたは 'JsonResponse'を使うときに' header'関数を追加したくありません。コメント行: 'header( 'Content-Type:application/json');' – bobo