ajaxfileupload 始终不执行success 只执行error方法;SyntaxError: expected expression, got '<

用ajaxFileUpload实现图片上传,图片已经成功上传,json也有返回,xml也正确配置。但是在jsp中ajaxFileUpload始终执行error方法。

用来碰巧将ajaxFileUpload 中的error方法改写成以下的样子(如果您已知道您报的是什么错,请无视这一部分。只是写给跟我一样的很白很白的小白)

            error:function(data,status,e){
                      alert(e);
                 }



再次执行后,出现上面的窗口。这下可开心了。因为之前问度娘说为何总是执行error时,得到的答案不是说jquery版本太高了,要1.4.2以下才能;不然就说是ajaxFileUpload源文件中要怎么怎么改。附上图,也许对你有用



针对  SyntaxError: expected expression, got '<    这个错误,我搜索到两篇文章,虽然不能直接解决我的问题,但也间接解决了我的问题。也许对你也有用

http://liwx2000.iteye.com/blog/1540321

http://witcheryne.iteye.com/blog/406574

看了第二篇文章后,我也开始用firebug 跟踪数据: 在error方法中添加console.info(data);

这是action返回给jsp的:


这是Firefox中看到的


看!果然也是因为多了<pre></pre>标签

这下就好办了,只要把标签去掉就行。去掉标签的方法上面两篇文章都有。因此不多说。我是按第二篇文章的方法处理的。

下面是代码

js

		function ajaxFileUpload(){
			$.ajaxFileUpload({
	 			url:"<%=path%>/file/imageupload.action?imgtype=lecturer",
	 			secureuri:false,
	 			dataType:"json",
	 			fileElementId:"uploadFile",
	 			success:function(data,status){
		 			console.info("3333333"+ data);
		 			    			   
					if(data.flag==true){
					alert(data.msg);
	 					$('#uploadDialog').dialog('close');
	 				}else{
	 					alert(data.msg);
	 					$('#uploadDialog').dialog('close');
	 				}
	 			},
	 			error:function(data,status,e){
	 				console.info("1111111"+data);
	 				alert(e);
	 			}
	     	});
	    }

ajaxFileUpload.js

    uploadHttpData: function( r, type ) {
        var data = !type;
        data = type == "xml" || data ? r.responseXML : r.responseText;
        // If the type is "script", eval it in global context
        if ( type == "script" )
            jQuery.globalEval( data );
        // Get the JavaScript object, if JSON is used.
        if ( type == "json" ) {
        	//以下是新增的代码
 			if(data.indexOf('<pre>') != -1) {
    			 data = data.substring(5, data.length-6);
			} 
 			//以上是新增的代码
            eval( "data = " + data );
        }
        // evaluate scripts within html
        if ( type == "html" )
            jQuery("<div>").html(data).evalScripts();
			//alert($('param', data).each(function(){alert($(this).attr('value'));}));
        return data;
    }

这里做新增代码处理即可,其他的不用改变

FileAction

public class FileAction extends ActionSupport{
	private static final long serialVersionUID = 8224125473579297368L;	
	
	private File file; //上传的文件
	private String fileFileName; //上传的文件名
    private JSONObject resultObj = null;

	public String imageupload() throws Exception {
		//这里实现上传图片功能
		String path = null;
		String message = null;
		boolean flag = false;
		JSONObject json = new JSONObject();
		json.put("msg", message);
		json.put("path", path);
		json.put("flag", flag);
		setResultObj(json);
		return SUCCESS;
	}
	


这里的resultObj不能定义为String,也就是说不能向前台传json字符串,否则data.flag即使是true,data.flag==true也不会成立

struts.xml

<span style="font-size:18px;">	<package name="file" extends="json-default" namespace="/file">
		<action name="imageupload" class="com.bossin.common.action.FileAction" method="imageupload">
			<result  name="success" type="json">
				 <param name="root">resultObj</param>
			</result>
		</action>
	</package></span>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值