簡體   English   中英

Symfony ajax 響應但帶有標頭:HTTP/1.0 200 OK 緩存控制:無緩存日期

[英]Symfony ajax Response but with headers: HTTP/1.0 200 OK Cache-Control: no-cache Date

我遇到了 Symfony 和 ajax 通話的麻煩。

我在 Windows 8 上的本地服務器中,XAMPP 1.8.2。

一切都很好,但是當我做出回應時,我在正確的文字下方有這個:

HTTP/1.0 200 OK Cache-Control: no-cache Date: Tue, 19 Nov 2013 14:58:18

為什么?

我的代碼:

在底部的 HTML (Twig) 中:

$.ajax({
url: "{{ path('score') }}",
data: { id: scoreid, value: value },
dataType: 'json',
type: 'POST',
success: function (data) {
    if(data.responseCode==200 ){           
    $('#score').html(data.score);
    $('#score').css("color","green");
            }
else if(data.responseCode==400){
    $('#score').html(data.score);
    $('#score').css("color","red");
     }
     else{
    alert("An unexpeded error occured.");
    $('#score').html(data);
      }
            },
  error: function (jxhr, msg, err) {
    $('#score').html('<span style="color:red">Error!</span>');
     }
});

Controller“得分”:

class scoreController extends Controller
{

    public function onepointAction(Request $request) {

        ....some logical...

            $points = self::pointsAction($id);

            $return=array("responseCode"=>200, "score"=>"Score: ".$num.".", "goal"=>"".$points);
        }

        else {

            $return=array("responseCode"=>400, "score"=>"No good!");
        }

        $return = json_encode($return);
        return new Response($return,200,array('Content-Type'=>'application/json'));
    }

    public function pointsAction($id) {

        ......some logical query... ended by:
            ->getQuery();

        $pointsOk = $query->getResult();

        $avgScore = $avgScore[0]["score_avg"];

        $numScore = $avgPoints[0]["score_count"];

        $points = ("Scores: ".$avgScore."Goals: ".$numScore);

        return new Response($points);
    }
}

我在哪里出錯?

我找到了解決我的大問題的方法:

->getContent()在$ points末尾的->getContent() = self::pointsAction($id);

這是很正常的,因為您正在渲染一個完整的“ Response”對象,其中也包含標題。

您所看到的

HTTP/1.0 200 OK Cache-Control: no-cache Date: Tue, 19 Nov 2013 14:58:18

是控制器答案中包含的標題。

如果要直接渲染JSon,則應考慮使用JSonResponse對象而不是Response對象。 (FQCN: Symfony\\Component\\HttpFoundation\\JsonResponse

為了澄清,這就是他的意思:

return response()->json([
            'status' => $status,
            'message' => $message,
        ], 200)->getContent();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM