Reputation: 1383
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);
$shopid=$input['Toshop'];
$shopforme=\App\shop::findOrFail($shopid);
$hefallow = \App\favirotshop::where("shop_id",$shopforme->id)
->where("user_id",$me->id)
->firstOrFail();
if(trim($input['mes'])){
$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);
$themessagee["shopName"]=$shopforme->name;
$themessagee["shopLogo"]=$shopforme->logo;
$themessagee["shopUrl"]=$shopforme->friendly_url;
$themessagee["MyName"]=$me->name;
$themessagee["MyPhoto"]=$me->photolink;
$themessagee["ShopOwner"]=$shopforme->foruser;
event(new ChatToNode($themessagee));
return response()->json($themessagee);
}
}
Jquery Side Code :
ShopID= $('#forShop').val();
$.ajax({
type: 'POST',
url : '/sendmessageboxShop',
dataType : "json",
data: {
Toshop: ShopID,
mes:mes
},
beforeSend: function () {
}
}).always(function() { // always executed
})
.fail(function(data) {
sweetAlert("Oops...", "there was erorr in connection", "error");
})
.done(function( $obj ) {
if($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>'+
'</div>'+
'</div>');
var bottomCoord = $('#chat-messages')[0].scrollHeight;
$('#chat-messages').slimScroll({scrollTo: bottomCoord});
$("#messageval").val('');
}
else {
console.log("no");
}
}
});
thanks in advance
Upvotes: 0
Views: 104
Reputation: 305
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();
$.ajax({
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.
Upvotes: 1