Support the ongoing development of Laravel.io →
posted 10 years ago
Requests

I 'm deleting records with ajax code (in a javascript function):

       $.ajax({
                type: "DELETE",
                url: "{{ URL::to('admin/categories/" +idtoedit +"')}}",
                success: function (result) {
                    alert('success');
                }
            });

The alert message does not show up. The records do get deleted. The code in my controller's destroy method *does get executed:

public function destroy($id) {       
       $category = Category::find($id);
       $category->delete();   
       return Redirect::to('admin/categories' )
                            ->with('message', 'Successfully updated page!');
    }

(I checked code execution with NetBeans) But redirection does not happen.

Also, I commented out all the code in the function and tried to echo something out. return 'hii'; Nothing was echoed.

Last updated 3 years ago.
0

You can not echo directly nor redirect inside ajax call. What you need is to echo or redirect using javascript/jquery inside ajax callback

So e.g. in controller you have

public function destroy($id) {       
       $category = Category::find($id);
       $category->delete();   
       return 'success'; //not the best value to return, but for illustration :)
    }

Then in jquery. This will alert the 'success' string from controller

$.ajax({
     type: "DELETE",
     url: "{{ URL::to('admin/categories/" +idtoedit +"')}}",
     success: function (result) {
        alert(result);  
     }
});

If you want to redirect, you must do it in jquery too

$.ajax({
    type: "DELETE",
    url: "{{ URL::to('admin/categories/" +idtoedit +"')}}",
    success: function (result) {
         window.location.replace('/admin/categories');  
    }
});
Last updated 10 years ago.
0

Thanks! It works fine!! Only one change needed: window.location.replace('categories'); instead of 'admin/categories'.

Last updated 10 years ago.
0

It worked fine in Laravel 4, but I'm trying it in Laravel 5 and doesn't work. I replaced and {{ }} with {!! !!}

url: "{!! URL::to('admin/categories/" +idtoedit +"')!!}",

This also does not work!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

panosss panosss Joined 18 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.