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

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 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型