【问题标题】:Form upload using jQuery (GWT)使用 jQuery (GWT) 上传表单
【发布时间】:2013-11-22 02:19:40
【问题描述】:

我需要能够在不重定向或刷新页面的情况下使用 GWT 进行表单上传,但是我的代码不再重定向,它仍然会刷新并且不会弹出警告框:

public static native void showUploadModal(String title, String hash)/*-{
        var target = '/files/" + hash + "/';
        $wnd.$("<div></div>")
            .html("<form id='form_upload' action='' method='post' enctype='multipart/form-data'><p>Select a file: <input type='file' name='file'/></p><input type='submit' id='form_submit' value='Attach'></form>")
            .appendTo("body")
            .dialog({
                title: title,
                modal: true,
                width: 400,
                hide: "fade",
                show: "fade",
                buttons: {
                    "Cancel": function() {
                        $wnd.$(this).dialog("close");
                    }
                }
            });    
        $wnd.$('#form_submit').submit(function() {
            // the script where you handle the form input.
            var url = '/files/" + hash + "/'; 
            $wnd.$.ajax({
                   type: "POST",
                   url: url,
                   data: $wnd.$('#form_upload').serialize(), // serializes the form's elements.
                   success: function(data)
                   {
                       alert(data); // show response 
                   }
                 });
            return false; // avoid to execute the actual submit of the form.
        });         
    }-*/; 

这段代码可能有什么问题?

【问题讨论】:

  • 任何客户端或服务器错误?它会进入这段代码吗?
  • 是的,点击按钮时调用该方法。它基本上显示了一个模式对话框,然后呈现表单上传

标签: java javascript jquery gwt jsni


【解决方案1】:

这里有一些细节有点奇怪:

var target = '/files/" + hash + "/';

var url = '/files/" + hash + "/'; 

这两行将其变量的值设置为/files/" + hash + "/,包括+ 和文本hash,而不是变量的值。可能不是你想的那样。 target 变量似乎没有被使用,但 url 被使用。

下一步:

alert(data); // show response 

相反,您的意思可能是$wnd.alert(data);

最后,像http://server.com/files/" + hash + "/ 这样的 url 可能不会发回成功代码,因此可能根本不会调用成功回调 - 一定要设置失败回调。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 2013-07-04
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多