0

Following is the code I use for an ajax call. While executing this it gives me an error xml parsing error syntax error line number 1 column 1 in fiebug. I viewed some questions stating the same problem and they suggested that there was some syntactical problem. I checked again but couldn't find out the real culprit. Please tell me what I am doing wrong.

  $.ajax({type: "GET",
            cache: false,
            url: 'url',
            data : 'param1='+ param1val+ '&param2='+param1val,
            dataType: 'json',
            success: function(Obj){
                if(Some Condition){
                   //Some Code
                }else{
                  //else code
                }
            },
   
        });

Here goes some controller code.

   @RequestMapping(value = "url", method = RequestMethod.GET)
    public @ResponseBody SomeObject url(@RequestParam(value="param1val") String  abc ,@RequestParam(value="param2val") String xyz) 
   { //some code}

Edit I added some debugging code to JS and the controller code as well. To my surprise, control executes successful first comes (in JS) and then goes into controller. Is it supposed to happen like this?

supernova
  • 1,762
  • 1
  • 14
  • 31
sandy
  • 1,153
  • 7
  • 21
  • 39
  • You should URL encode the req params in your javascript. See http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript & http://xkr.us/articles/javascript/encode-compare/ – Zaki Sep 23 '12 at 12:58
  • Reading the link,gives me impression that encodeURI is use in js.I am using jquery ajax,do I need to do that? – sandy Sep 23 '12 at 13:25
  • 1
    Are you sure you don't getting json instead of xml? Could you post an example of your data, which produces the error? – Stan Sep 23 '12 at 17:45
  • From controller I do return a custom object.Shouldn't I use json than.Though I've tried with datatype=xml as well.It was no help either.:( – sandy Sep 23 '12 at 17:59
  • 1
    What format is used for the object, and what format is set in http-header? Anyway, without an exapmle of data which produces the error, it will be just hit-and-miss guessing. – Stan Sep 23 '12 at 18:19

1 Answers1

1

Firefox shows this error if the response type is not set correctly. It tries to parse the response as XML. To fix it, set the response type for what you are sending back to the client, for example for JSON with Spring:

@RequestMapping(value = "url", 
                method = RequestMethod.GET, 
                produces = "application/json")
Mikko
  • 1,877
  • 1
  • 25
  • 37