gpt4 book ai didi

javascript - angularjs http post 与 Angular 和 GAE

转载 作者:行者123 更新时间:2023-11-28 06:46:05 27 4
gpt4 key购买 nike

我需要一个小修复。我只需要使用 angularjs 将我的数据(评论)发布到数据存储(GAE),但它还没有发生。以下 angularjs“post”或 html 有什么问题?

Angular :

$scope.addComment = function() {

var form_comment = $scope.formFields.comment

var payload = {
comment: form_comment
}

$http({
method: 'POST',
url: '/exp'
}).then(function successCallback(response) {

$scope.comments.push(payload);


}, function errorCallback(response) {

});

};

HTML:

{% extends "home.html"%}
{% block content %}


<div ng-controller="commentController" class="formcontent">
<div class ="container">

<form ng-submit="addComment()" method="post" id="frmComment">

<textarea ng-model="formFields.comment" id="comment" name="commento" class="form-control status-box sameline" rows="2" placeholder="Recommend Colin"></textarea>
</form>
<div class="button-group pull-right sameline">
<p class="counter">140</p>
<button form ="frmComment"class="btn btn-primary" type="submit">Post</button>
</div>
</div>

<div>
<ul class="posts">
<li ng-repeat = "c in comments">
{< c.comment >}
</li>
</ul>
</div>

</div>
{% endblock %}

Python:

class expHandler(webapp2.RequestHandler):
def get(self):
title="Colin_MK: Experience"
recommendations = Recommendation.query()
self.response.out.write(json.dumps([rec.to_dict() for rec in recommendations]))
template_vars = {'title': title, 'recommendations': recommendations}
template = JINJA_ENVIRONMENT.get_template('/exp.html')
self.response.out.write(template.render(template_vars))

def post(self):
r = json.loads(self.request.body)

new_comment = Recommendation(comment=r['comment'])
new_comment.put()

app = webapp2.WSGIApplication([
('/', MainHandler),
('/bio', bioHandler),
('/exp', expHandler)
], debug=True)

最佳答案

post 方法的签名如下:

post(url, data, [config]); 

所以你还应该包括有效负载。尝试这样的事情:

$http.post('/exp', payload).then(...)

此外,在 Promise 的 then() 方法上,您应该发送对该方法的引用:

      .then(function(response) {
$scope.comments.push(payload);
}, function(response) {

});

关于javascript - angularjs http post 与 Angular 和 GAE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33409142/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com