iteye_15020 2009-06-07 20:25
浏览 329
已采纳

jquery form插件success回调函数无法执行,返回json数据无法读取

 

// JavaScript Document
// prepare the form when the DOM is ready 
$(document).ready(function() { 

   var show = function(data){
   alet('ffff');
   alert(data);
   }

    // bind to the form's submit event 
    $('#upload').ajaxForm({
         target:'#uploadMsg',
         dataType:  'json', 
         beforeSubmit:showRequest,
         success:showResponse 
    });
    
    
}); 
 
// pre-submit callback 
function showRequest(formData, jqForm) { 
     alert('beforeSubmit');
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    //var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    for (var i=0; i < formData.length; i++) { 
        if (!formData[i].value) { 
            alert('Please select a file'); 
            return false; 
        } 
    }  
   
    //alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(data)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 alert('success');
 alert(data);
    
} 


    

 

服务器端用的是struts2.1,一个uploadAction

 

现在能实现上传

页面代码如下:

 

<div class="upload">
                    <s:form  id="upload" action ="ajax/upload!topicUpload.do" method="post" enctype="multipart/form-data">
                    <div><input id="topicId" name="topicId" type="hidden" value="<s:property value="topicId"/>"/></div>
                    <div><input id="type" name="type" type="hidden" value="2"/></div>
                    <div><label class="adminleft">附件:</label><s:file name ="myFile" label ="upload File" /> <span class="loading">...</span><s:submit value="上传"/> </div>
                    </s:form>
                    <div class="fileUrl"></div>
                    <div><label id="uploadMsg">你还没有上传任何文件</label></div>
                    </div>

             </div>

 jquery用的是1.3,form插件是最新的,文件上传后,返回的一个upload!topicUpload.do的json文件,还让你保存,就是不执行回调函数,实在搞不懂了,

 

struts的配置

 

 <!-- ajax模块 -->   
    <package name="ajax"  extends="json-default">
         <action name="upload" class="uploadAction">
          <interceptor-ref name="fileUpload">
              <param name="maximumSize">20480000</param> 
            </interceptor-ref> 
           <interceptor-ref name="defaultStack"/> 
           <result type="json">
           </result>
       </action>    
    </package> 

不知道有没有谁也遇到过这样的问题

  • 写回答

1条回答 默认 最新

  • wanghaolovezlq 2009-06-08 12:51
    关注

    有上传文件的表单是不能这样做的,因为ajax没法发送整个到服务器,这点一定要记住

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的