Symfony ajax响应但带标题:HTTP / 1.0 200 OK Cache-Control:no-cache Date
i'm in troubles with Symfony and an ajax call.
I'm in Local server on Windows 8 with XAMPP 1.8.2.
Every works good, but when i take response i have this, below the right text:
HTTP/1.0 200 OK Cache-Control: no-cache Date: Tue, 19 Nov 2013 14:58:18
Why?
My codes:
In HTML (Twig) at the bottom:
$.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 "score":
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);
}
}
Where I make error?
我遇到了Symfony和ajax调用的麻烦。 p>
I 在使用XAMPP 1.8.2的Windows 8上的本地服务器中。 p>
一切正常,但当我接受回复时,我有这个,在正确的文本下面: p> \ n
HTTP / 1.0 200 OK缓存控制:无缓存日期:星期二,2013年11月19日14:58:18
code> pre>
为什么? p>
我的代码: p>
在底部的HTML(Twig)中: p>
$。 ajax({
url:“{{path('score')}}”,
data:{id:scoreid,value:value},
dataType:'json',
type:'POST',
success:function (数据){
if(data.responseCode == 200){
$('#score')。html(data.score);
$('#scre')。css(“color”,“ 绿色“);
}
else if(data.responseCode == 400){
$('#score')。html(data.score);
$('#score')。css(”color “,”“red”);
}
其他{
alert(“发生了一次未出现的错误。”);
$('#scre')。html(data);
}
},\ n错误:function(jxhr,msg,err){
$('#score')。html('&lt; span style =“color:red”&gt;错误!&lt; / span&gt;');
}
});
code> pre>
控制器“得分”: p>
class scoreController扩展Controller
{
公共函数onepointAction(请求$请求){
......一些逻辑...
n $ points = self :: pointsAction($ id);
$ return = array(“responseCode”=&gt; 200,“score”=&gt;“得分:”。$ num。“。”,“目标 “=&gt;”“。$ points);
}
else {
$ return = array(”responseCode“=&gt; 400,”score“=&gt;”不好!“);
}
$ return = json_encode($ return);
返回新的响应($ return,200,array('Content-Type'=&gt;'application / json'));
}
公共函数pointsAction($ id){
...... ......一些逻辑查询...结束于:
- &gt; getQuery();
$ pointsOk = $ query-&gt; getResult();
$ avgScore = $ avgScore [0] [“score_avg”];
$ numScore = $ avgPoints [0] [“score_count”];
$ points =(“分数 :“。$ avgScore。”目标:“。$ numSc ore);
返回新的响应($ points);
}
}
code> pre>
我在哪里犯错? p>
I found the solution to my big problem:
->getContent()
at the end of $points = self::pointsAction($id);
It's quite normal as you're rendering a full "Response" object, which contains headers too.
What you see as
HTTP/1.0 200 OK Cache-Control: no-cache Date: Tue, 19 Nov 2013 14:58:18
are the headers contained in the controller answer.
If you want to render JSon directly you should then consider using the JSonResponse
object instead of the Response
one. (FQCN : Symfony\Component\HttpFoundation\JsonResponse
)