0

以下のコードは、最初に以前のコメントを表示しようとしています。次に、送信ボタンをクリックすると、新しいコメントを表示して更新します。

2 番目の AJax 呼び出しは正常に機能します。しかし、最初の Ajax 呼び出しは何もしません (「/show」は有効な要求です。最初の呼び出し「/comment」は「/show」を呼び出します)。

ここに私のコードスニペットがあります:

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

前もって感謝します

4

1 に答える 1

0

はい、成功ブロック内で console.log/alert を実行しましたが、そこに到達していないようです。上記のように、どちらの ajax 機能もほとんど同じです。最初のものはサーバー呼び出し 'show' を作成し、2 番目のものはサーバー呼び出し 'comment' を作成し、'comment' の処理後にサーバー呼び出し 'show' にリダイレクトします。2 番目の ajax 呼び出しは正常に機能します。

これが私のHandlebar Templateコードです:

 <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 に答える