Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
POST方法无法在angularjs中获取500个内部服务器错误_Angularjs_Forms_Api_Post_Yii - Fatal编程技术网

POST方法无法在angularjs中获取500个内部服务器错误

POST方法无法在angularjs中获取500个内部服务器错误,angularjs,forms,api,post,yii,Angularjs,Forms,Api,Post,Yii,我正试图通过使用POST方法发送数据来创建一个用户。我得到这个500内部服务器错误。实际上,当用户被创建时,它的一些属性存储在用户表中,三个属性存储在PROFILES表中。我使用高级Rest客户端测试了这个,它在那里工作得很好,但是当我在angular中应用相同的精确方法时,我得到了这个500错误。我附上一些屏幕截图给你真实的图片。API是在YII框架中编写的 我的控制器 $scope.createuser = function (username, password, email, super

我正试图通过使用POST方法发送数据来创建一个用户。我得到这个500内部服务器错误。实际上,当用户被创建时,它的一些属性存储在用户表中,三个属性存储在PROFILES表中。我使用高级Rest客户端测试了这个,它在那里工作得很好,但是当我在angular中应用相同的精确方法时,我得到了这个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 -->      
创建用户

带*的字段是必需的

请修复以下输入错误:

  • dummy
    • 密码 电子邮件*

      超级用户* 不 对 地位* 不活跃 活跃的 禁止 名字* 图画* 姓* 创造

原料药

公共函数actionCreate(){
开关($\u GET['model']){
//获取相应模型的实例
“创建”案例:
$model=新用户;
$profile=新配置文件;
打破
违约:
$this->_sendsresponse(501,sprintf($u GET['model'])未为模型%s实现模式创建);
Yii::app()->end();
}
//尝试将POST值指定给属性
foreach($\作为$var=>$value发布){
//如果($var=='profile'){
////var_dump($value);
//             }
//模型是否具有此属性?如果没有,则引发错误
如果($model->hasAttribute($var)|$var=='profile')
$model->$var=$value;
否则{
//var_dump($var);
$this->_sendsresponse(500,sprintf($s参数不允许用于模型%s',$var,$_GET['model']);
}
}
//尝试保存模型
如果($model->save()){
$profile->user\u id=$model->id;
$profile->firstname=$\u POST['profile']['firstname'];
$profile->lastname=$\u POST['profile']['lastname'];
$profile->picture=$\u POST['profile']['picture'];
$profile->validate();
$profile->save();
$this->_sendsresponse(200,CJSON::encode($model));
}
否则{
//发生错误
$msg=“错误”;
$msg.=sprintf(“无法创建模型%s”,$\u GET['model']);
$msg.=“
    ”; foreach($model->errors as$attribute=>$attr\u errors){ $msg.=“
  • 属性:$Attribute
  • ”; $msg.=“
      ”; foreach($attr\u errors作为$attr\u error) $msg.=“
    • $attr_error
    • ”; $msg.=“
    ”; } $msg.=“
”; $this->\u sendResponse(500,$msg); } }
尝试在控制器中使用CJSON类,例如:
使用yii\helpers\Json

在我在响应中检查的inspect元素中,我得到了整个页面的代码。请检查运行时或服务器日志中的错误和堆栈跟踪。把它包括在问题中。当我删除$profile->user\u id=$model->id$profile->firstname=$\u POST['profile']['firstname']$profile->lastname=$\u POST['profile']['lastname']$简介->图片=$\u发布['profile']['picture'];这些行被发布了,但是我的API目的失败了。没有上面的行,数据保存在用户表中,但是当我删除这些行配置文件时,表属性没有存储为什么这个东西在高级rest客户端中工作,而不是在Angular中。
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);
        }
    }