Support the ongoing development of Laravel.io →
posted 9 years ago
Requests
Last updated 2 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 9 years ago.
0

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

Last updated 9 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.

© 2024 Laravel.io - All rights reserved.