angularjs http post with angular and GAE

时间:2015-10-29 08:23:18

标签: javascript python angularjs

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

角:

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

1 个答案:

答案 0 :(得分:0)

post方法的签名如下:

post(url, data, [config]); 

因此您还应该包含有效负载。尝试这样的事情:

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

此外,在承诺的 then()方法中,您应该发送对该方法的引用:

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

      });