ajaxfileupload 始终不执行success 只执行error方法;SyntaxError: expected expression, got '<
2015-04-13 15:29
549 查看
用ajaxFileUpload实现图片上传,图片已经成功上传,json也有返回,xml也正确配置。但是在jsp中ajaxFileUpload始终执行error方法。
用来碰巧将ajaxFileUpload 中的error方法改写成以下的样子(如果您已知道您报的是什么错,请无视这一部分。只是写给跟我一样的很白很白的小白)
再次执行后,出现上面的窗口。这下可开心了。因为之前问度娘说为何总是执行error时,得到的答案不是说jquery版本太高了,要1.4.2以下才能;不然就说是ajaxFileUpload源文件中要怎么怎么改。附上图,也许对你有用
针对 SyntaxError: expected expression, got '< 这个错误,我搜索到两篇文章,虽然不能直接解决我的问题,但也间接解决了我的问题。也许对你也有用
http://liwx2000.iteye.com/blog/1540321
/article/3940722.html
看了第二篇文章后,我也开始用firebug 跟踪数据: 在error方法中添加console.info(data);
这是action返回给jsp的:
这是Firefox中看到的
看!果然也是因为多了<pre></pre>标签
这下就好办了,只要把标签去掉就行。去掉标签的方法上面两篇文章都有。因此不多说。我是按第二篇文章的方法处理的。
下面是代码
js
ajaxFileUpload.js
这里做新增代码处理即可,其他的不用改变
FileAction
这里的resultObj不能定义为String,也就是说不能向前台传json字符串,否则data.flag即使是true,data.flag==true也不会成立
struts.xml
用来碰巧将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
/article/3940722.html
看了第二篇文章后,我也开始用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>
相关文章推荐
- ajaxfileupload 始终不执行success 只执行error方法;SyntaxError: expected expression, got '<
- ajaxfileupload 始终不执行success 只执行error方法;SyntaxError: expected expression, got '<
- ajaxfileupload上传文件出现SyntaxError:unexpected token <错误
- ajaxFileUpload plugin上传文件 chrome、Firefox中出现SyntaxError:unexpected token <
- ajaxFileUpload插件上传文件 返回 syntaxError :unexpected token <
- ajaxFileUpload()中的success和error中的回调函数不执行的备注
- 使用ajaxfileupload.js上传文件成功之后,没有执行success方法
- ajaxFileUpload plugin上传文件 chrome、Firefox中出现SyntaxError:unexpected token <
- AJaxFileUpload 文件上传<pre>,json字符串为空解决方法
- [ERROR] mysqld: File './mysql-bin.~rec~' not found (Errcode: 13)解决方法
- ajaxFileUpload返回的json数据带<pre>标签,导致无法执行success后面的代码
- supervisorctl报错"error: <class 'socket.gaierror'>, [Errno -2] Name or service not known"的解决方法
- mysqldump执行时Got error: 1045: Access denied for user 'root'@'localhost' (using password: YES) when trying to connect
- 当使用JQuery的"$",抛异常Uncaught TypeError: Object #<Object> has no method 'ajax'
- 关于Error 1 error C1189: #error : "include 'stdafx.h' before including this file这个错误的解决方法
- ajaxfileupload返回json带<pre>
- fatal error C1001: INTERNAL COMPILER ERROR(compiler file 'msc1.cpp', line 1786)解决方法
- mac /include/c++/v1/iterator:341:10: fatal error: '__debug' file not found #include <__debug>
- pb9.0 编绎成DLL类型时,提示"Error opening file 'c:/windows/system32/cgen/ent32.h" 问题的解决方法
- 【Error】Python:SyntaxError: Non-ASCII character '\xe5'解决方法