2016-11-18 33 views
0

私はサーバー側とクライアント側の間に問題があります。私はPHP Symfonyでサーバー側にRest APIを持っています。 サーバー側:私は、適切なJSONオブジェクトを得たapplication/x-www-form-urlencodedPHP 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を使用しようとしています。

+3

あなたは 'JsonResponse'を使うときに' header'関数を追加したくありません。コメント行: 'header( 'Content-Type:application/json');' – bobo

答えて

0

この問題を遅くするにはあまりにもdificultだった、と背中のXMLHttpRequestを持って、AJAX応答を取得するとき、私は呼び出されるコールバック関数を使用しているので、私は、jQueryのAjaxのを捨てます。

function sendAjaxRequest(method, url, data, contentType, callback) 
{ 
    var response 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
     if (this.readyState == 4 && this.status == 200) { 
      response = xhttp.responseText; 
      callback(JSON.parse(response)); 
     } 
    }; 
    xhttp.open(method, url, true); 
    xhttp.setRequestHeader("Content-Type", contentType); 
    xhttp.send(data); 

    return response; 
} 

主な問題:応答ハンドラ機能は、常にAjax応答が到着したより早く実行されます。

1

ローダーコンテナの更新を成功ハンドラに移動します。

function sendAjaxRequest(method, url, data, contentType) { 
    var response; 
    $.ajax({ 
     method: method, 
     url: url, 
     data: data, 
     dataType: 'json', 
     contentType: contentType, 
     success: function(msg) { 
      document.getElementById("loader-container").innerHTML = JSON.parse(msg); 
     } 
    }); 
} 
+0

基本的に私はXMLHttpRequestを使ったのと同じ解決策を出しました。それは良い解決策でもありますが、それは私にとっては少しばかりだったので、これを古いソリューションに変更しました。 – PumpkinSeed

関連する問題