Reputation: 363
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?
Upvotes: 3
Views: 7767
Reputation: 113
For clarification, this is what he meant:
return response()->json([
'status' => $status,
'message' => $message,
], 200)->getContent();
Upvotes: 0
Reputation: 363
I found the solution to my big problem:
->getContent()
at the end of $points = self::pointsAction($id);
Upvotes: 9
Reputation: 1333
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
)
Upvotes: 1