0
您好,我製作了rails api和angularJs項目,並且發現了以下錯誤。Angularjs with rails API rest post 400(壞請求)
angular.js:11821 POST http://localhost:3000/people 400(錯誤請求)
而且我找不到原因。
以下代碼。
Rails的控制器
# POST /people
def create
@person = Person.new(person_params)
if @person.save
render json: @person, status: :created, location: @person
else
render json: @person.errors, status: :unprocessable_entity
end
end
AngularJS
$scope.SendData = function() {
// use $.param jQuery function to serialize data from JSON
var data = $.param({
name: $scope.name,
age: $scope.age,
gender:$scope.gender,
lonlat:$scope.lonlat
});
var config = {
headers : {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
}
$http.post('http://localhost:3000/people', data, config)
.then(function (data, status, headers, config) {
$scope.PostDataResponse = data;
})
.catch(function (data, status, header, config) {
$scope.ResponseDetails = "Data: " + data +
"<hr />status: " + status +
"<hr />headers: " + header +
"<hr />config: " + config;
});
};
的Html
<div ng-app="myApp" ng-controller="person">
<div class="modal-body">
<div class="form-group">
<label for="exampleInputEmail1">Name:</label>
<input type="text" class="form-control" name="name" ng-model="name" placeholder="Email">
</div>
<div class="form-group">
<label for="exampleInputEmail1">age:</label>
<input type="text" class="form-control" name="age" ng-model="age" placeholder="Email">
</div>
<div class="form-group">
<label for="exampleInputEmail1">gender:</label>
<input type="text" class="form-control" name="gender" ng-model="gender" placeholder="Email">
</div>
<div class="form-group">
<label for="exampleInputEmail1">lonlat:</label>
<input type="text" class="form-control" name="lonlat" ng-model="lonlat" placeholder="Email">
</div>
<button ng-click="SendData()"
class="btn btn-default">Submit</button>
{{ PostDataResponse }}
謝謝你能聽
請發佈相關日誌。 400錯誤代碼表示請求錯誤,當您向服務器發送格式不正確的請求時。查看瀏覽器控制檯日誌和Rails日誌以獲取有關您的請求的信息。 –
POST http:// localhost:3000/people 400(Bad Request) (匿名)@ angular.js:11821 n @ angular.js:11582 g @ angular.js:11292 (匿名)@ angular.js :16104 $ @ EVAL angular.js:17378 $ @消化angular.js:17191 $ @適用angular.js:17486 (匿名)@ angular.js:25177 射頻@ angular.js:3487 d @ angular.js:3475。在我的Chrome控制檯 –
「status」:400,「error」:「Bad Request」,「exception」:「#\ u003cActionController :: ParameterMissing:param is missing or the value is empty:person \ u003e」,「traces」 :{「Application Trace」:[{「id」:1,「trace」:「app/controllers /people_controller.rb:47:in' person_params'」}}在螢火蟲日誌 –