使用jQuery(GWT)上传表单

Form upload using jQuery (GWT)

本文关键字:表单 GWT jQuery 使用      更新时间:2023-09-26

我需要能够在不重定向或刷新页面的情况下使用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.
        });         
    }-*/; 

此代码可能有什么问题?

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

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,可能不会发回成功代码,因此可能根本不会调用成功回调——请确保也设置了失败回调。