3

I have a Google App Engine that has a form. When the user clicks on the submit button, AJAX operation will be called, and the server will output something to append to the end of the very page where it comes from. How, I have a Django template, and I intend to use jquery. I have the following view:

<html>
<head>
<title></title>
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/scripts.js"></script>

</head>
<body>
welcome
<form id="SubmitForm" action="/" method="POST"> 
<input type="file" name="vsprojFiles" />
<br/>
<input type="submit" id="SubmitButton"/>
</form>

<div id="Testing">
{{thebest}}
</div>

</body>
</html>

Here's the script in scripts.js:

$(function() {
    $("#SubmitForm").click(submitMe);
});

var submitMe = function(){
    //alert('no way');
    var f = $('#SubmitForm');
    var action = f.attr("action");
    var serializedForm = f.serialize();
  $.ajax( {
        type: 'post',
        data: serializedForm,
        url:  form_action,
        success: function( result ) {
          $('#SubmitForm').after( "<div><tt>" +
                                     result +
                                     "</tt></div>" );
        }
      } );

    };

And here's my controller code:

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.api.urlfetch_errors import *
import cgi
import wsgiref.handlers
import os
import sys
import re
import urllib
from django.utils import simplejson

class MainPage(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'Index.html')
        template_values={'thebest': 'thebest'}
        tmplRender =template.render(path, template_values)
        self.response.out.write(tmplRender)
        pass

    def Post(self):
        print >>sys.__stderr__,'me posting'
        result = 'grsgres'
        self.response.out.write(simplejson.dumps(result))

As you can see, when the user clicks on the submitbutton, the controller method Mainpage.post will be called.

Now I want to display the content of the 'result' variable right after the form, how can I do it?

4

2 回答 2

2

如果无法测试代码,您的结果是什么?你检查过 AJAX 调用返回的结果吗?我建议您使用 Firebug 运行 Firefox,并将 AJAX 结果记录到 Firebug 控制台以查看您得到的结果:

//...
        success: function( result ) { 
        console.log( result );
      $('#SubmitForm').after( "<div><tt>" + 
// ...

您还可以使用 Firebug 的 Net 面板查看来回传递的内容。

另外,“simplejson.dumps(result)”会导致什么结果?

于 2008-11-05T21:53:18.323 回答
1

这是我的成功功能的一个例子

success: function(json){
                $('#gallons_cont').html(json['gallons']);
                $('#area_cont').html(json['area']);
                $('#usage_cont').html(json['usage'])
                $('#results_json').show('slow');            
            },

请注意,您必须使用 firebug 或类似的东西进行调试,因为可能存在一些序列化问题,这些问题会抛出并出错,但除非您使用 firebug 之类的东西或实现 .ajax 错误,否则无法查看

于 2009-05-07T02:26:37.493 回答