现在的位置: 首页 > 综合 > 正文

jsp 解决ajax返回数据 parsererror

2013年10月13日 ⁄ 综合 ⁄ 共 1166字 ⁄ 字号 评论关闭

今天用ajax提交数据的时候,发现server接收正常,数据已经提交。

返回数据老报parsererror

最后查了一下说书返回的数据有问题。即

out.println("{'xx':'xxxxxx'}")

有问题。 然后查资料 调试一下 我发现我server提交的是一个jsp

<!doctype html>
<%
	String path = request.getContextPath();
	pageContext.setAttribute("path", path);
	ReplyServlet reply = new ReplyServlet();
	String action = request.getParameter("action");
	if ("reply".equals(action)) {
		reply.postReply(request);
		//response.setContentType("text/json;charset=UTF-8");
		out.write("{\"result\":\"true\",\"msg\":\"true\"}");
		out.flush();
		return;
	}

然后我调了一下

dataType改为 text  查看显示的值

<!doctype html>

{"result":"ok","msg":"true"}

-------------------------------------------------------至此原因已经找到

function submit_ajax(){
		//alert(xx.toJSONString());
		$.ajax( {     
		    url:'index.jsp',// 跳转到 action     
		    data:{     
		    	   action : "reply",
		           tid : $("#tid").val(),     
		           energy : $("#game_coin").val(),     
		           content : $("#content").val()     
		    },     
		    type:'post', 
		    //contentType: "application/json; charset=utf-8",
		    //cache:false,     
		    dataType:'text',     
		    success:function(data) {  
		    	alert(data);
		        if(data.msg =="true" ){     
		            // view("修改成功!");     
		            alert("修改成功!");     
		            window.location.reload();     
		        }else{     
		            alert(data.msg);     
		        }     
		     },     
		     complete : function(XMLHttpRequest, textStatus, errorThrown) {     
		          //alert(XMLHttpRequest.status);
                  //alert(XMLHttpRequest.readyState);
                  alert(textStatus);
		     }
		}); 

抱歉!评论已关闭.