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;
        $tag->save();

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

}
Best Answer (As Selected By jozenaiser)
Ruffles

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

jozenaiser
Ruffles
Ruffles
1 year ago (297,250 XP)

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

MaverickChan

show me your ajax code please

vipin93
vipin93
1 year ago (61,380 XP)

follow @Ruffles and 500 errors is your backend errors laravel

jozenaiser

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();

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

        $.ajax({
                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) {
                    console.log(res);
                    if(res.status == 'ok') {
                        $('#edit-check').slideUp(300).fadeIn(400);
                        $('#modal-edit-tag').modal('hide').delay(800);
                        console.log('Tag updated');
                    }
                }
            }).fail(function(res) {
                console.log('Fail at updating tag');
            });
    }

This is the ajax code @MaverickChan

Ruffles
Ruffles
1 year ago (297,250 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.

Example:

// 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.

simondavies

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.