500 http error - AJAX

Published 1 year ago by jozenaiser

Hello guys.

I'm having some problems at updating a model via ajax.

If I comment the specific line where I save the model ($tag->save()), no error is thrown, but once I uncomment it, I get that error which has been driving me crazy.

Can somebody help me?

This is the code:

public function edit(Request $request) {

    $this->validate($request, [
        'name' => 'required|max:75',
    if($request->ajax()) {
        $tag = Tag::find($request->id);
        $tag->name = $request->name;

        if($request->type == 'hashtag') {
            Session::flash('message', 'Se ha actualizado correctamente el hashtag!');   
            Session::flash('message', 'Se ha actualizado correctamente la categoria!');
        return response()->json(['status' => 'ok']);
        return redirect()->back();
     return response()->json(['status' => 'ok']);

Best Answer (As Selected By jozenaiser)

What's the error? Check your Chrome Dev Tools Network tab for more details.

1 year ago (289,160 XP)

What's the error? Check your Chrome Dev Tools Network tab for more details.


show me your ajax code please

1 year ago (60,860 XP)

follow @Ruffles and 500 errors is your backend errors laravel


Hey guys, thanks for helping me. @vipin93 @Ruffles @MaverickChan. I really appreciate it.

I guess that by using Chrome Dev Tools Network tab, as @Ruffles said, I found out my error. I wasn't importing Session Facade and had a problem with the url specified in the ajax code. I'm new to Ajax so I hadn't used Chrome Tools that much before, now I see it's quite helpful.

I have one more question, is there any way to set the url better than the way I do it in here:

function saveTagUpdated() { var id = $('#edit-tag-id').val(); var name = $('#edit-tag-name').val(); var type = $('#edit-tag-type').val();

            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf- token"]').attr('content')

                type: 'POST',
                url: id + '/edit',   // THIS IS THE CURRENT WAY I DO IT
                dataType: 'json',
                data: {
                    id : id,
                    name : name,
                    type : type,
                    _token : '{{ Session::token() }}'
                success: function(res) {
                    if(res.status == 'ok') {
                        console.log('Tag updated');
            }).fail(function(res) {
                console.log('Fail at updating tag');

This is the ajax code @MaverickChan

1 year ago (289,160 XP)

When I want to pass an ID to the JQuery script, I usually use HTML5 data attributes on the button and just pick up the value in the script.


// The Blade file
<button type="button" id="button" data-id="{{ $model->id }}"> Button </button>

// The Script
var id = $('#button').data('id');

Most of the time I just hardcode the URL in the script but when I want to make it dynamic, I also use data attributes for the resource name similar to the example above.

Another idea that came to mind just now is that you could pass the whole URL via a data attribute, something like data-url, using Laravel's route() helper.


I tend to pass the desired URL within the data attribute as somehting like:

 ... data-url="{{route('name',$named-.id)}}">NAME</button>

Then us js to get the data URL, and set that to a var, with what js method you like, kQuery/VanillaJS etc similar to how @Ruffles mentioned

Please sign in or create an account to participate in this conversation.