无法通过$ http.post方法发送数据,出现500个内部服务器错误

乌斯曼一世

我被困在这个问题已经两个小时了,不知道为什么我会收到这个错误。该API是使用Yii PHP框架编写的,我正在开发前端。我收到来自Yii API的响应错误。

//Create a new Model Action 
public function actionCreate()
{
    switch($_GET['model'])
    {
        // Get an instance of the respective model
        case 'reviewbusinesses':
            $model = new ReviewBusiness;                    
            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) {
        // Does the model have this attribute? If not raise an error
        if($model->hasAttribute($var))
            $model->$var = $value;
        else
            $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())
        $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 );
    }
}

角度代码

<form ng-submit="add_review()">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="booking-item-rating">
                                    <ul class="icon-list icon-group booking-item-rating-stars">
                         <input-stars max="5" ng-model="reviewbusiness.rating"></input-stars>

                                    </ul>

                                </div>
                                <div class="form-group">
                                    <label>Review Text</label>
                                    <textarea class="form-control" rows="6" ng-model="reviewbusiness.review"></textarea>
                                </div>
                                <input type="submit" class="btn btn-primary" value="Leave a Review" />
                            </div>

                        </div>
                    </form>

角度控制器

recentControllers.controller('BusinesspageController', ['$scope', '$http', '$routeParams', function ($scope, $http, $routeParams) {
        $scope.id = $routeParams.itemId;

        $scope.reviewbusiness = {};
        $scope.add_review = function(){
            $scope.reviewbusiness.user_id = 1;
            $scope.reviewbusiness.business_id = 42;


            console.log($scope.reviewbusiness);
            //return;
            $http({
                method: 'POST',
                url: 'http://ba.dev/reviewbusinesses/reviewbusinesses/',
                data: $scope.reviewbusiness,
                headers: {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  }

            })
                    .success( function(response){
                        console.log(response);
                    })
                    .error( function(response){
                       console.log(response); 
                    });
        };



    }]);

我得到的错误

<h1>Error</h1>Couldn't create model <b>reviewbusinesses</b><ul><li>Attribute: user_id</li><ul><li>User cannot be blank.</li></ul><li>Attribute: business_id</li><ul><li>Business cannot be blank.</li></ul><li>Attribute: review</li><ul><li>Review cannot be blank.</li></ul><li>Attribute: rating</li><ul><li>Rating cannot be blank.</li></ul></ul>

当我使用GET方法时。当我使用GET方法回复在此处输入图片说明

scaisEdge

Yii中的actionCreate似乎使用GET方法,因此不要评估通过post方法提交的数据。

尝试在ajax中使用GET metodh

       $http({
            method: 'GET',
            url: 'http://ba.dev/reviewbusinesses/reviewbusinesses/',
            data: $scope.reviewbusiness,
            headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 kotlin 通过改造向烧瓶服务器发送数据时出现 400 http 错误

无法从 C# 调用 HTTP POST 方法 - 内部服务器错误

POST 请求上的 Http 500 内部服务器错误

file_get_contents无法打开流:HTTP请求失败!HTTP / 1.1 500内部>服务器错误

为什么我无法访问tomcat中的服务CXF:HTTP状态500-内部服务器错误

无法通过HTTP请求发送Cookie

Django不通过电子邮件报告内部服务器错误(HTTP状态码500)

无法访问 HTTP 服务器中的 POST 数据

无法通过angularjs的$ http模块将数据发布到服务器

LotusScript:通过POST方法HTTP发送数据

无法在 Angular 4 中通过 HTTP 发送 POST 请求

OkHttp:无法通过POST方法将数据发送到服务器

如何修复内部服务器错误?HTTP错误:500

尝试通过 Web api 调用下载文件时,如何解决“发送 HTTP 标头后服务器无法设置状态”错误?

无法通过内部/专用网络上的浏览器使用Node.js http服务器

REACTJS xhr.js:177 POST http://localhost:3000/registerUser 500(内部服务器错误)

POST http://localhost:8000/api/projects/ 500(内部服务器错误)

Servlet-HTTP状态500内部服务器错误

Android中的Http 500内部服务器错误

GlassFish Http状态500-内部服务器错误

通过SSH服务器发送HTTP请求

如何编码图像以通过Python HTTP服务器发送?

如何通过Java HTTP服务器发送图像

通过HTTP GET将数据发送到PHP服务器

Istio:无法通过HTTP / HTTPS使用网关访问服务

无法通过服务将HTTP响应返回给ViewModel(UWP MVVM)

通过HTTP POST发送文件UploadFile错误:无法建立SSL / TLS安全通道的信任关系

Jetty客户端无法通过HTTP / 2连接到Jetty服务器

无法通过以下网址访问Django服务器:http://127.0.0.1:8000 /