0

The code below is attempting to first display previous comments, and then on submit button click, refresh by showing the newer comment.

The second AJax call works fine. But the first Ajax call does nothing ('/show' is a valid request - and the first call '/comment' calls '/show')

Here is my code snippet:

<script>
   $(document).ready(function() {
        var source   = $("#login_template");
        var srcHTML =source.html();
        var login_template = Handlebars.compile(srcHTML);
        source   = $("#comment_template");
        srcHTML =source.html();
        var comment_template = Handlebars.compile(srcHTML);
        // First time call to server to get the comments
        $.ajax({ // ajax call starts
            type: "post",
            url: "/show", 
            contentType: 'application/json',
            success: function(data,textStatus,jqXHR) 
            {
              data = JSON.parse(data);
              var html = login_template(data);
              $("#login_content").html(html);
              html = comment_template(data);
              $("#comment_content").html(html);
            }
         });        

        $("#comment_button").click(function(event) {
          $.ajax({ // ajax call to send comment to server and refresh with newer comment
            type: "post",
            url: "/comment", 
            contentType: 'application/json',
            data:  JSON.stringify(
                {
                  'source' : source.value,
                  'comment': comment.value,
                }),
            success: function(data,textStatus,jqXHR) 
            {
              data = JSON.parse(data);
              var html = login_template(data);
              $("#login_content").html(html);
              html = comment_template(data);
              $("#comment_content").html(html);
            }
         });        
      });
   });
 </script>

Thanks in advance

4

1 回答 1

0

是的,我在成功块内做了 console.log/alert ,但它似乎没有到达那里。正如您在上面看到的,两个 ajax 功能几乎相同。第一个使服务器调用“show”,第二个使服务器调用“comment”,然后在处理“comment”后重定向到服务器调用“show”。第二个 ajax 调用工作正常。

这是我的车把模板代码:

 <script id="login_template" type="text/x-handlebars-template">
          logout={{logout}}
          login={{login}}
          {{#if user}}
            {{user.nickname}}!  
            [<a href="{{logout}}"><b>sign out</b></a>]
          {{else}}
            Guest  
            [<a href="{{login}}"><b>sign in</b></a>]
          {{/if}}
    </script>

    <script id ="comment_template" type="text/x-handlebars-template"> 
        <h2>Top 10 Most Recent Guestbook Entries</h2>
          no of greetings {{greetings.length}}
          {{#each greetings}}
             greetings!
            <br>
              <small>[<i>{{date.ctime}}</i>]</small>
              <b>
              {{#if author }}
                <code>{{author.nickname}}</code>
              {{else}}
                <i>anonymous</i>
              {{/if}}
              </b>
              wrote:
              {{comment}}
          {{/each}}
    </script>

这是我在 HTML 正文中的 DOM 元素:

  <div id="login_content"></div>
  <div id="comment_content"></div>

这是服务器代码:

class ShowComment(webapp2.RequestHandler):
     def get(self):
        user = users.get_current_user()
        greetings = Greeting.all().order('-date').fetch(10)
        results = {
            'user':      user,
            'greetings': greetings,
            'login':     users.create_login_url(self.request.uri),
            'logout':    users.create_logout_url(self.request.uri),
        }
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write( JSONUtils.GqlEncoder().encode(results) )

class Guestbook(webapp2.RequestHandler):
  def post(self):
    Jguest_data = json.loads(self.request.body) 
    greeting = Greeting()
    for gb_name,gb_comment in Jguest_data.iteritems():

        user = users.get_current_user()
        if user:
           greeting.author = user
        greeting.content = gb_comment
        greeting.put()
        self.redirect('/show')

app = webapp2.WSGIApplication([('/', MainPage),
                              ('/comment', Guestbook),
                              ('/show',ShowComment)
                               ],
                              debug=True)

谢谢一米。

于 2013-02-14T14:54:53.000 回答