0

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.

RESPONSE.PHP

This function is on 403 line.

 public function setContent($content)
    {
        if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable(array($content, '__toString'))) {
            throw new \UnexpectedValueException(sprintf('The Response content must be a string or object implementing __toString(), "%s" given.', gettype($content)));
        }

        $this->content = (string) $content;

        return $this;
    }
Rinalds Gudriks
  • 65
  • 2
  • 12
  • Your PHP error message seems to be really clear about what your issue is. – Julien Oct 02 '15 at 10:41
  • Explain please then, and tell me how to fix it. – Rinalds Gudriks Oct 02 '15 at 10:46
  • I don't know where I set boolean, if I define `var list = {name: 'Test'};` what is string and then I send `data: list` what is string as well. So where I send boolean? Please explain me how you understand that message, because I don't understand where i send boolean. – Rinalds Gudriks Oct 02 '15 at 10:50
  • I'm guessing it originates from this line: `$response = Lists::findorfail($id)->update($request->all());` If you're passing a boolean (`$response`) to `return Response($response, 201);`, then `Response` is telling you not to pass a boolean, pass a string instead. – Jared Farrish Oct 02 '15 at 10:55
  • Ok, and how to change `$response` to string then? – Rinalds Gudriks Oct 02 '15 at 11:04
  • Minimally, `return Response(json_encode($response), 201);`, which will produce the text `true` or `false`. – Jared Farrish Oct 02 '15 at 11:14
  • Omg, thanks so much guys, `return Response (var_export($response), 201);` and all works fine. – Rinalds Gudriks Oct 02 '15 at 11:15

0 Answers0