I'm using json_encode throughout my project without issue, except in one instance.
I make an ajax call from one page, as I do in others, and the resulting json appends a 1 to the end of the string for some odd reason.
My return string looks like this
{
"overtime": "yes"
}1
What could be causing this? I have literally mented everything out of the class that returns this string and I simply have the following code.
$reservation = ['overtime' => 'yes'];
return json_encode($reservation, JSON_PRETTY_PRINT);
My ajax request looks like this
$.ajax({
type: 'POST',
url: "{{ URL::action('Controllers\\PurchasesController@calculateReservation') }}",
data: { 'arrive' : arrive, 'depart' : depart},
dataType: 'json',
success: function(response) {
alert(response);
}
});
The alert doesn't fire and doesn't display anything as the json is invalid with the 1 appended to the end of the string.
I'm using json_encode throughout my project without issue, except in one instance.
I make an ajax call from one page, as I do in others, and the resulting json appends a 1 to the end of the string for some odd reason.
My return string looks like this
{
"overtime": "yes"
}1
What could be causing this? I have literally mented everything out of the class that returns this string and I simply have the following code.
$reservation = ['overtime' => 'yes'];
return json_encode($reservation, JSON_PRETTY_PRINT);
My ajax request looks like this
$.ajax({
type: 'POST',
url: "{{ URL::action('Controllers\\PurchasesController@calculateReservation') }}",
data: { 'arrive' : arrive, 'depart' : depart},
dataType: 'json',
success: function(response) {
alert(response);
}
});
The alert doesn't fire and doesn't display anything as the json is invalid with the 1 appended to the end of the string.
Share Improve this question edited Mar 25, 2015 at 18:23 Joshua asked Mar 25, 2015 at 18:20 JoshuaJoshua 7919 silver badges24 bronze badges 2-
1
In my experience, I normally
echo
a response, rather thanreturn
it. Try doing that and if necessary, callingdie()
immediately afterwards. Does that help? – Tom Fenech Commented Mar 25, 2015 at 18:23 - Wow. Yes, that did the trick, thank you! Very odd as I simply return the json_encode throughout the rest of my project without any issue. – Joshua Commented Mar 25, 2015 at 18:25
1 Answer
Reset to default 9You should echo
the response from your controller, rather than returning it:
echo json_encode($reservation, JSON_PRETTY_PRINT);
In some scenarios (for example using WordPress), it is also necessary to call die()
afterwards, as well.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745081226a4610138.html
评论列表(0条)