Ajax调用抛出无效字符错误

问题描述 投票:0回答:1

我正在使用Struts 1.2开发Java应用程序。当我对Struts动作进行AJAX调用时,我遇到了阻塞错误。

成功地调用了struts动作getInfos.html,但是此后,当我进行AJAX调用时,在控制台中出现以下错误:

无效字符/解析错误

数据变量采用正确的JSON格式。为什么会触发此错误?

我已经在线浏览了所有类似的问题,但我不知道为什么它会触发无效的字符错误。

$.ajax({
  type: "POST",
  url: "getInfos.html",
  dataType: "json",
  async: false,
  cache: false,
  data: {
    Code: "code1",
    type: "type",
    mand: "mand",
    signature: "signature"
  },
  success: function(data) {
    console.log('succes');
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
    console.log('my error is : ' + errorThrown);
  }
});

在处理ajax请求的execute方法中,我正在使用会话调用属性

    final String code = (String) request.getAttribute("code");
    final String signature = (String) request.getAttribute("signature");
    final String type= (String) request.getAttribute("type");
               /*
            Making a call to a webservice using the attributes bellow, 
             using **response** Object
               */
     if (reponse != null && 
      (CodeReponseHttp.OK.equals(reponse.getCodeReponse()))) {
                jsonObj.put(SUCCESS_CALL, true);

            } else {
                jsonObj.put(SUCCESS_CALL, false);
            }

    return new JsonResult(jsonObj);

但是它们设置为null;这意味着ajax数据不会传递到请求中,当我调试execute方法并为这些属性显式设置值时,一切正常。

new JsonResult(jsonObj)是一个通用类,其构造函数接受JSONObject

javascript jquery json ajax struts
1个回答
0
投票

像Rory McCrossan一样,它可能是您得到的响应不是json,并且您的代码需要json响应

当我评论dataType参数时,它工作正常

$.ajax({
    type : "POST",
    url : "getInfos.html",      
    //dataType : "json",
    async: false,
    cache: false,
    data: JSON.stringify({
    Code : "code1",
    type : "type",
    mand : "mand",
    signature : "signature"}),
    success : function(data){
        console.log('succes');
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {

         console.log('my error is : ' + errorThrown);
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.