POST物品无法在angularjs中获得500内部服务器错误

时间:2016-09-05 09:27:04

标签: angularjs forms api post yii

我正在尝试使用POST方法发送数据来创建用户。我收到此500内部服务器错误。实际上,场景是当用户被创建时,其某些属性存储在USER表中,而三个属性存储在PROFILES表中。我使用Advanced Rest Client对它进行了测试,它在那里工作得很好,但是当我在角度中应用相同的精确方法时,我得到了这个500错误。我附上一些屏幕截图给你真实的照片。 API是用YII框架编写的

我的控制器

$scope.createuser = function (username, password, email, superuser, status, firstname, lastname, picture) {
            $scope.formData = {'username': username, 'password': password, 'email': email, 'superuser': superuser, 'status': status, 'profile[firstname]': firstname, 'profile[lastame]': lastname, 'profile[picture]': picture};
            console.log($scope.formData);
            $http({
                method: 'POST',
                url: 'http://xx.dev/users/create',
                data: $.param($scope.formData),
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            })
                    .success(function (data) {
                        console.log(data);
                        if (!data.success) {
                            // if not successful, bind errors to error variables
                            console.log('okay');
                        } else {
                            // if successful, bind success message to message
                            $scope.message = data.message;
                        }
                    });

        };

我的表格

<h1>Create User &nbsp; <a href="" class="btn btn-primary btn-sm">Manage User</a>
    <a href="" class="btn btn-primary btn-sm">Manage Profile Fields</a>
    <a href="" class="btn btn-primary btn-sm">List User</a></h1>

<div class="form">

    <form name="createUser" novalidate>
        <p class="note">Fields with <span class="required">*</span> are required.</p>

        <div id="user-form_es_" class="alert alert-block alert-danger" style="display:none"><p>Please fix the following input errors:</p><ul><li>dummy</li></ul></div><div class="col-md-6"> <!--add this line in order to give this form a reasonable look -->
            <div class="row">
                <div class="form-group">
                    <div>
                        <input type="text" ng-model="username" class="input-20 form-control">
                    </div>
                </div>      

            </div>

            <div class="row">
                <div class="form-group">
                    <label class="control-label" for="User_password">password</label>
                    <div>
                        <input type="password" ng-model="password" class="input-60 form-control">
                    </div>
                </div>      
            </div>

            <div class="row">
                <div class="form-group">
                    <label class="control-label required" for="User_email">E-mail <span class="required">*</span></label>
                    <div>
                            <input type="email" ng-model="email" class="input-60 form-control">
                        <p id="User_email_em_" style="display:none" class="help-block"></p>
                    </div>
                </div>      
            </div>

            <div class="row">
                <div class="form-group">
                    <label class="control-label required" for="User_superuser">Superuser <span class="required">*</span></label>
                    <div>
                        <select class="form-control" ng-model="superuser" id="User_superuser">
                            <option value="0">No</option>
                            <option value="1">Yes</option>
                        </select>
                    </div>      
                </div>
                </div>

                <div class="row">
                    <div class="form-group">
                        <label class="control-label required" for="User_status">Status <span class="required">*</span></label>
                        <div>
                            <select class="form-control" ng-model="status" id="User_status" required>
                                <option value="0">Not active</option>
                                <option value="1">Active</option>
                                <option value="-1">Banned</option>
                            </select>
                        </div>
                    </div>      
                </div>  
                <div class="row">
                    <div class="form-group">
                        <label class="control-label required" for="Profile_firstname">First Name <span class="required">*</span></label>
                        <div>
                            <input type="text" ng-model="firstname" id="Profile_firstname" class="input-60 form-control">
                        </div>
                    </div>      

                </div>
                <div class="row">
                    <div class="form-group">
                        <label class="control-label required" for="Profile_picture">Picture <span class="required">*</span></label>
                        <div>
                            <input type="text" ng-model="picture" id="Profile_picture" class="input-60 form-control">
                        </div>
                    </div>      

                </div>
                <div class="row">
                    <div class="form-group">
                        <label class="control-label required" for="Profile_lastname">Last Name <span class="required">*</span></label>
                        <div>
                            <input type="text" ng-model="lastname" id="Profile_lastname" class="input-60 form-control" ng-placeholder="lastname" required>
                        </div>
                    </div>      
                </div>
                <div class="gap-small"></div> <!-- add the gap-->
                <div class="row buttons">
                    <button class="btn btn-default" type="submit" name="yt0" ng-click="createuser(username , password , email , superuser, status , firstname , lastname , picture)">Create</button>    </div>
            </div> 
            <!--eneded here -->
    </form>
</div><!-- form -->      

enter image description here

API

public function actionCreate() {
        switch ($_GET['model']) {
            // Get an instance of the respective model
            case 'create':
                $model = new User;
                $profile = new Profile;
                break;
            default:
                $this->_sendResponse(501, sprintf('Mode <b>create</b> is not implemented for model <b>%s</b>', $_GET['model']));
                Yii::app()->end();
        }
        // Try to assign POST values to attributes
        foreach ($_POST as $var => $value) { 
//             if($var=='profile'){
//                 //var_dump($value);
//             }
            // Does the model have this attribute? If not raise an error
            if ($model->hasAttribute($var) || $var=='profile')
                $model->$var = $value;
            else{
                //var_dump ($var);
                $this->_sendResponse(500, sprintf('Parameter <b>%s</b> is not allowed for model <b>%s</b>', $var, $_GET['model']));
            }
        }
        // Try to save the model
        if ($model->save()){
            $profile->user_id = $model->id;
            $profile->firstname= $_POST['profile']['firstname'];
            $profile->lastname= $_POST['profile']['lastname'];
            $profile->picture= $_POST['profile']['picture'];
            $profile->validate();
            $profile->save();
            $this->_sendResponse(200, CJSON::encode($model));
        }
        else {
            // Errors occurred
            $msg = "<h1>Error</h1>";
            $msg .= sprintf("Couldn't create model <b>%s</b>", $_GET['model']);
            $msg .= "<ul>";
            foreach ($model->errors as $attribute => $attr_errors) {
                $msg .= "<li>Attribute: $attribute</li>";
                $msg .= "<ul>";
                foreach ($attr_errors as $attr_error)
                    $msg .= "<li>$attr_error</li>";
                $msg .= "</ul>";
            }
            $msg .= "</ul>";
            $this->_sendResponse(500, $msg);
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试在控制器中使用CJSON类,例如:use yii\helpers\Json;