【问题标题】:Symfony ajax Response but with headers: HTTP/1.0 200 OK Cache-Control: no-cache DateSymfony ajax 响应但带有标头:HTTP/1.0 200 OK 缓存控制:无缓存日期
【发布时间】:2013-11-19 15:16:07
【问题描述】:

我遇到了 Symfony 和 ajax 调用的麻烦。

我在带有 XAMPP 1.8.2 的 Windows 8 上的本地服务器中。

每个都很好,但是当我收到回复时,我在右文本下方有这个:

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>');
     }
});

控制器“分数”:

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);
    }
}

我在哪里出错了?

【问题讨论】:

  • 嗨@ginolin!此行中的 $num 是什么:$return=array("responseCode"=&gt;200, "score"=&gt;"Score: ".$num.".", "goal"=&gt;"".$points);!您可以在 $return 之前发布 $points 转储吗?
  • $points (pointsAction) 的转储是这样的:string(53) "Scores: 5.8021Goals: 96" 转储中没有标头。我把var_dump 放在return new Response($points); 之前,我不是从第一个控制器的同一根树枝上取的,我只从pointsAction 控制器的简单路径中取它。
  • 您的代码似乎非常正确,因为您在 dataType 参数中使用了 json 值。尝试在控制器的操作中使用JsonResponse。就我而言,我使用了这个类。
  • 尝试查看JSONStringfy 以在不使用dataType 参数的情况下将data 转换为json
  • 我找到了解决我的大问题的方法:-&gt;getContent() at the end of $points = self::pointsAction($id);

标签: php ajax http symfony header


【解决方案1】:

这很正常,因为您正在渲染一个完整的“响应”对象,其中也包含标题。

你所看到的

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)

【讨论】:

  • 亲爱的朋友@YoannCh,我将代码更改为://$return = json_encode($return); //return new Response($return,200,array('Content-Type'=&gt;'application/json')); return new JsonResponse($return);,但我采用相同的标题(也使用:Content-Type application/json)。同样在pointsAction controller 中使用此更改的代码。怎么可能?
  • 我明白了:如果你只使用return $return会发生什么? (或return $points)
  • 如果我将简单的 return $points 放入 pointsAction 我会从 Symfony 收到此错误:The controller must return a response。如果我在 onepointAction Controller 中输入简单的返回值,我会从 Ajax error: function (jxhr, msg, err) 得到错误。
  • 你在控制器操作上使用注释,例如@Template ?
【解决方案2】:

我找到了解决我的大问题的方法:

-&gt;getContent() 在 $points 末尾 = self::pointsAction($id);

【讨论】:

    猜你喜欢
    • 2013-10-07
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 2021-10-20
    • 2020-07-23
    • 2017-12-29
    • 1970-01-01
    相关资源
    最近更新 更多