如何在提交html表单时使用jQuery获取响应?

时间:2012-10-17 12:43:17

标签: javascript jquery forms blobstore blobstorage

我正在使用BlobStore来保存图像。我有一个jsp页面。该页面的代码如下:

 <%@ page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory" %>
 <%@ page import="com.google.appengine.api.blobstore.BlobstoreService" %>
 <%
 BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
 %>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html>
   <head>
     <meta name="description" content="My web page" />
    <title>My Web</title>
    <script type="text/javascript" src="jquery-1.8.0.js"></script>
    <script src="jquery-mainpage.js"></script>
   </head>

<body>
            <form method="post" id="upload_img_form" name="upload_img_form" action="<%= blobstoreService.createUploadUrl("/imagedao") %>" enctype="multipart/form-data">
                <input id="upload_img_form_input" name="upload_img_form_input" type="file">
                <input id="upload_img_submit" type="submit" name="upload_img_submit" value="Upload >>" />
            </form>
</body>
</html>

在jquery-mainpage.js中我有以下代码:

 $("#upload_img_form").submit(function(e){
        e.preventDefault();
        var upload_img_val = $("#upload_img_form_input").val();
        if(upload_img_val == ''){
                alert('Please choose a file!');
        }else{
            $.ajax({
               type: 'POST',
               url: url_to_blobstore,
               data: {upload_img_form_input:upload_img_val},
               success: function(result) {
                 console.log('upload_img_form result: '+result);
            }
         });
       }
});

我收到以下异常:

 java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2786)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:71)
at javax.mail.internet.MimeMultipart.readTillFirstBoundary(MimeMultipart.java:244)
at javax.mail.internet.MimeMultipart.parse(MimeMultipart.java:181)
at javax.mail.internet.MimeMultipart.getCount(MimeMultipart.java:109)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet.handleUpload(UploadBlobServlet.java:145)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet.access$000(UploadBlobServlet.java:68)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet$1.run(UploadBlobServlet.java:111)
at java.security.AccessController.doPrivileged(Native Method)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet.doPost(UploadBlobServlet.java:108)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.appengine.tools.appstats.AppstatsFilter.doFilter(AppstatsFilter.java:141)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:35)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:60)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:94)

为什么我得到这个例外。代码有问题吗? 我希望在提交此表单时得到回复。所有jQuery代码都在jquery-mainpage.js中。是否有任何jQuery方法可以给我回复?

提前致谢。

1 个答案:

答案 0 :(得分:0)

如果你想要的是有权访问客户端脚本的响应,只需使用jquery提交表单:

http://api.jquery.com/jQuery.post/

响应回调可以访问响应。

或者您可以使用完整的Forms插件来管理来自客户端的表单提交:

http://www.malsup.com/jquery/form/