skip to Main Content

I am working on a Java application using Struts 1.2. I am facing a blocking error when I make an AJAX call to a Struts action.

The struts action, getInfos.html, is called successfully but after that when I make the AJAX call I get the following error in the console:

Invalid Character/parsing error

The data variable is a correct JSON format. Why would it trigger this error?

I’ve gone through all the similar questions online but I don’t know why it’s triggering an invalid character error.

$.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);
  }
});

In the execute method that is handling the ajax request, i am calling the attributes using the request

    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);

But they are set to null; which means that the ajax data is not passed into the request, when I debug the execute method and I explicitly set values to these attributes everything works fine.

new JsonResult(jsonObj) is a generic class with a constructor that accepts a JSONObject

2

Answers


  1. Chosen as BEST ANSWER

    The problem had been solved, after debugging, the response type was not a JSON since there is a redirection to an error page if an exception is thrown, the exception was thrown because the data attributes were null, and it turned out that they are parametres not attributes, so getting the parameters solved the problem.

    request.getParameter("code");
    

    thank you all for your collaboration.


  2. Like Rory McCrossan Comment it could be the response you got is not a json and your code expect a json response

    When i comment dataType param it work fine

    $.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);
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search