vendredi 2 octobre 2015

500 Internal Server error when PUT in angularjs

PROBLEM

Hello, long time ago I got 500 Internal Server Error when I use method PUT in angularjs (I'm trying to update list name), but still don't know how to fix it and make update work properly. At the moment i'm manually set value to update. I got that error, but when I refresh page, then lists name is changed to that value. Seems like angular don't work properly and after put, it don't give .success, but give me .error, because of what I can't use my GET method.

So at the moment I can set list name for example "Grocery List", hit button edit, hit enter (because I set new list name value in angular manually) and get error. After page refresh I will get same "Grocery List", but with new name "Test". I need single page application, so I can't use refresh, but GET method after PUT method don't work. Any ideas how to make it work? Please help me!

ERROR

When I'm changed list name and hit enter in console shows error PUT http://localhost/anydo/anydocopy/anydocopy/public/lists/1 500 (Internal Server Error). I checked Network->Preview in google chrome and there shows UnexpectedValueException in Response.php line 403: The Response content must be a string or object implementing __toString(), "boolean" given.. So anybody know where is problem?

CODE

routes.php

Route::group(['middleware' => 'cors'], function () {
    Route::get('tasks', 'TasksController@index');
    Route::get('lists', 'ListsController@index');
    Route::get('lists/{id}', 'ListsController@show');
    Route::post('lists', 'ListsController@store');
    Route::put('lists/{id}', 'ListsController@update');
    Route::post('lists/{id}', 'TasksController@store');
    Route::delete('lists/{id}', 'ListsController@delete');
    });

midleware cors.php

class Cors
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $response->headers->set('Access-Control-Allow-Origin', '*');
        $response->headers->set(
            'Access-Control-Allow-Headers',
            'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, x-xsrf-token, X-Requested-With'
        );
        $response->headers->set('Access-Control-Allow-Credentials', 'true');
        $response->headers->set('Access-Control-Allow-Methods', '*');
        return $response;
    }
}

controller

public function update($id, CreateListsRequest $request)
{
    $response = Lists::findorfail($id)->update($request->all());

    return Response($response, 201);
}

angular

  $scope.updatel = function($event){
            console.log($event.keyCode);
            console.log($scope.editlist);
            if ($event.keyCode == 13) {
                var list = {
                  name: 'Test'
                };

                $http({
                    method: 'PUT',
                    url: 'http://localhost/anydo/anydocopy/anydocopy/public/lists/1',
                    data: list
                })
                    .success(function () {
                        console.log('true');
                        $http({
                            method: 'GET',
                            url: 'http://localhost/anydo/anydocopy/anydocopy/public/lists'
                        })
                            .success(function (d) {
                                console.log(d);
                                $scope.listsdata = d;
                            });
                    })
                     .error(function () {
                     console.log(list);
                     console.log('false');

                    });

html

 <div ng-repeat="lists in listsdata.lists">
                    <div id="DIV_24" close-on-outside-click="div.popup_information">
                        <button ng-click="lists.show = !lists.show" id="MORE_BUTTON">:</button>
                        <div class="popup_information" ng-show="lists.show">
                                <button id="DELETE_BUTTON" ng-click="del_list(lists)">X</button>
                                <button id="EDIT_BUTTON" ng-click="edbut.show = !edbut.show">E</button>
                        </div>
                        <input type="text" id="edit" ng-model="editlist" ng-show="edbut.show" ng-keydown="updatel($event)" onkeydown="hideshow(document.getElementById('edit'))" class="form-control" style="font:24px bold;" value="{{lists.name}}" />
                        <a href="#/{{lists.id}}">
                            <div id="DIV_25">
                                <label class="test" style="font-weight: normal" ng-show="!edbut.show" close-on-outside-click="test">{{lists.name}} </label>
                            </div>
                            <div id="DIV_26">
                            </div>
                        </a>
                    </div>

                </div>

I know that mby too much code, but I don't understand how to fix this error and where I did mistake, so I just give you all code I'm working with. If need any other code, please ask in comments.

WHAT I TRYED ALREADY

I just change .success to .error and .error to .success and now all work like it must, but I still get same error. At the moment GET method in angular work when I get .error, what is wrong. Mby someone could help me and make solution why I get error after PUT method.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire