Jehad Ahmad Jaghoub Jehad Ahmad Jaghoub - 1 year ago 80
jQuery Question

Laravel 5.2 JSON response sometime start with Zero

I have the following problem on laravel 5.2 Json response

as some time response start with 0 leading the json response which make me error in client side only with the user interaction
JSON response

0{"message":"now shall be readed at least in javascript","fromuser":7,"fromShopid":null,"to_shop_id":9,"to_user_id":null,"status":0,"updated_at":"2016-11-16 14:20:45","created_at":"2016-11-16 14:20:45","id":58,"Created":1479295245,"shopName":"\u041a\u043e\u0440\u0430\u0431\u043b\u0438\u043a","shopLogo":"logo1475487308.jpg","shopUrl":"korablik","MyName":"Evgenia Kondrakhina","MyPhoto":"logo14717092787.jpg","ShopOwner":1}

this response happens sometimes not rarely for example it can happen after close browser and open it again or just happen without reason

Laravel side Code :

public function sendmessageboxShop (Request $Request){

$input= $Request->all();
$me = \App\User::find(\Auth::user()->id);
$hefallow = \App\favirotshop::where("shop_id",$shopforme->id)


$themessagee= \App\shopChat::create([
'message' => $input['mes'],
'fromuser' => $me->id,
'fromShopid' => null,
'to_shop_id' => $shopforme->id,
'to_user_id' => null,
'status' => 0,


$themessagee["Created"] =strtotime($themessagee->created_at);

event(new ChatToNode($themessagee));
return response()->json($themessagee);



Jquery Side Code :

ShopID= $('#forShop').val();

type: 'POST',
url : '/sendmessageboxShop',
dataType : "json",
data: {
Toshop: ShopID,
beforeSend: function () {

}).always(function() { // always executed

.fail(function(data) {
sweetAlert("Oops...", "there was erorr in connection", "error");
.done(function( $obj ) {

{ if($("div").find("[data-logforShop='" + ShopID + "']").is(":visible")){

$('#chat-messages').append('<div class="message right">'+
'<img src="/profilepics/'+$obj.MyPhoto+'" />'+
'<div class="bubble">'+
$obj.message +
'<div class="corner"></div>'+
'<span id="timeagodiv" class="modaltime" data-livestamp="'+$obj.created_at+'"></span>'+

var bottomCoord = $('#chat-messages')[0].scrollHeight;
$('#chat-messages').slimScroll({scrollTo: bottomCoord});

else {


thanks in advance

Answer Source

I'm not a laravel user, but in case the problem is from laravel core, you can fix in your javascript by adding a data filter to the request.

ShopID = $('#forShop').val();

    type: 'POST',
    url: '/sendmessageboxShop',
    dataType: "json",
    data: {
        Toshop: ShopID,
        mes: mes
    },dataFilter: function(raw_json){
        return raw_json.replace(/[0-9]+\{/, "{");
....the rest of your code

I hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download