[英]Why my POST return [object Object]
I'm new in Angular js and I don't know if my POST is working.我是 Angular js 的新手,我不知道我的 POST 是否有效。 It returns an [object Object]!它返回一个[object Object]! What kind of error this?这是什么错误? I mean if POST is working is there something wrong with the form?我的意思是如果 POST 正在工作,表格有问题吗?
//Activity controller
.controller('ActivityCtrl', function($scope, $rootScope, $state, $ionicLoading, $ionicScrollDelegate, PostService, $http, AuthService) {
var user = AuthService.getUser();
$http.get("http://hannation.me/api/buddypressread/activity_get_activities_grouped/?userid=" + user.data.id)
.success(function(data) {
$scope.activities = data.activities;
});
$scope.addActivity = function(){
//
var dataObj = {
new_activity : $scope.new_activity
};
$http.post('http://hannation.me/api/userplus/activities_post_update/?key=57f211a0354d7&cookie='
+ user.cookie + '&content=' + dataObj).success(function(data, status, headers, config) {
$scope.message = data;
});
$scope.new_activity='';
};
})
<form class="row">
<div class="col col-80 content col-center">
<input class="new-comment-message" type="text" placeholder="Leave a comment..." ng-model="new_activity"
name="new_activity"></input>
</div>
<div class="col col-20 button-container col-center">
<button class="button button-clear send" type="submit" ng-click="addActivity()">
Send
</button>
</div>
</form>
First, and mainly because this really bugs me... use the params
property for query parameters and don't use the deprecated success
method.首先,主要是因为这真的让我感到困扰……使用params
属性作为查询参数,不要使用已弃用的success
方法。 Using params
ensures your query parameters are sanitised for use in a URL (see encodeURIComponent()
).使用params
可确保您的查询参数经过清理以在 URL 中使用(请参阅encodeURIComponent()
)。
$http.get('http://hannation.me/api/buddypressread/activity_get_activities_grouped/', {
params: { userid: user.data.id }
}).then(function(response) {
$scope.activities = response.data.activities;
});
Secondly, this documentation (which I assume is correct) indicates you should be using a GET
request, not POST
and content
appears to be a string so your second request should look like其次,这个 文档(我认为是正确的)表明您应该使用GET
请求,而不是POST
并且content
似乎是一个字符串,因此您的第二个请求应该看起来像
$http.get('http://hannation.me/api/userplus/activities_post_update/', {
params: {
key: '57f211a0354d7',
cookie: user.cookie,
content: $scope.new_activity
}
}).then(function(response) {
// not sure about this, the documentation doesn't indicate there's a response
console.log('response data', response.data);
});
Answer lie within your question.答案就在你的问题中。 You are signing object to your message you should do like this您正在签署反对您的消息,您应该这样做
$http.post('http://hannation.me/api/userplus/activities_post_update/?key=57f211a0354d7&cookie='
+ user.cookie + '&content=' + dataObj).success(function(data, status, headers, config) {
$scope.message = data.message;
});
Here i have assigned one of the value that i am getting from post request.在这里,我分配了我从发布请求中获得的值之一。 To know what are the values lie within your response object, console your response object and assign appropriate value to your $scope.message.要了解您的响应对象中的值是什么,请控制您的响应对象并为您的 $scope.message 分配适当的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.