Bi Híp
Bi Híp

Reputation: 103

Don't run window.location.href in ajax laravel 5.2

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

Answers (2)

D Coder
D Coder

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

Giedrius Kiršys
Giedrius Kiršys

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

Related Questions