Reputation: 103
i have problem, when i use ajax, code larvel run and i can get result from ajax but this don't run window.location.href = "http://stackoverflow.com";
,
$('.promotion').click(function(){
var id= $(this).attr('id');
var url= $(this).attr('href');
$.ajax({
url:"link-"+id+"-"+url+"",
type:"GET",
cache:false,
data:{'id':id,'url':url,_token:$(this).data('token')},
dataType:"json",
success: function(data){
if(data=='oke'){
window.location.href = "http://stackoverflow.com";
}
}
});
return false;
});
countController
public function count($id){
$db= DB::table('promotions')->where('id','=', $id)->get();
DB::table('promotions')->where('id','=', $id)->update(['count_pm' => $db[0]->count_pm +1]);
echo 'oke';
}
Upvotes: 0
Views: 1075
Reputation: 572
Remove data type:json and try this
public function count($id){
$db= DB::table('promotions')->where('id','=', $id)->get();
DB::table('promotions')->where('id','=', $id)->update(['count_pm' => $db[0]->count_pm +1]);
return response()->json(['result' => 'oke']);
}
And jquery code will be
if(data.result == 'oke'){
window.location.href = "http://stackoverflow.com";
}
Upvotes: 1
Reputation: 5324
Your script trying to convert server response to JSON object because of this parameter: dataType:"json",
. Just remove it and You are good to go!
Or: change Your return from controller - return array which will be converted to JSON in front end.
Upvotes: 1