Usman Iqbal Usman Iqbal - 3 months ago 23
AngularJS Question

cannot send data through $http.post methood, getting 500 internal server error

It has been two hours that I am stuck in this problem, do not know why I am getting this error. The API is written in Yii PHP framework and I am developing the front end. I am getting the error in response which is coming from 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 );
}
}


Angular code

<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>


Angular controller

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);
});
};



}]);


error i am getting

<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>


When I use GET method.
When I use GET method
Response
enter image description here

Answer

The actionCreate in Yii seems use GET metho so don't eval the data that you submit by post method ..

try using GET metodh in your ajax

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