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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler