JQuery ajax调用一直回调error函数

时间:2023-12-12 09:33:08

使用jquery的ajax调用,发现一直回调error函数,ajax调用代码如下,后台返回是正确的,为什么会报错呢?

  1.  var descValue = $('#descEditArea').val();
  2.             descValue = descValue.replace(/\n/g, '<br/>');
  3.             var url = "/network/KpiDescServlet";
  4.             $.ajax({
  5.                 url:url,
  6.                 type:"post",
  7.                 data:{rm:Math.random(),kpiId:"<%=kpiId%>",kpiType:"<%=kpiType%>",kpiDesc:descValue},
  8.                 dataType:"json",
  9.                 success:function(data){
  10.                     alert("修改成功");
  11.                     $("#desc").html(descValue);
  12.                      cancle();
  13.                 },
  14.                 error:function(){
  15.                     alert("修改失败");
  16.                             cancle();
  17.                 }
  18.             });  
后来查找资料才发现,后台返回处设置的为ContentType"text/xml",而前台要求的是json,后台返回的不是一个正确的json,所以报错,修改方法为把前台的dataType修改为text
  1. response.setContentType("text/xml;charset=UTF-8");
  2. response.setHeader("Cache-Control", "no-cache");
  3. PrintWriter out = null;
  4. out = response.getWriter();
  5. out.println(result);