我遇到这个问题已经有两个小时了,不知道为什么我会收到这个错误。 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>