2

Hi SO.

I have a weird problem that I've been struggling with all day long.

I have my Angular JS module (App), and a controller. In that controller, I inject a factory, which handles my $http.post method. (Which i call "doSearch"). So far so good.

On the back-end I have a spring-mvc rest api class. It looks like this:

@Controller
@RequestMapping("/api/filesearch")
public class FileSearchAPI {

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO doSearch(@RequestBody NewPerfectSearchDTO searchDTO) {
          System.out.println(searchDTO.getIdentifier());
          SolrService solrService = new SolrService();
          //return solrService.query(searchDTO);
          return new ResultDTO();
     }
}

And this is the function that sends some JSON, that will be mapped to a JAVA POJO/DTO:

doSearch: function(searchParams) {
        $http.post("/np/api/filesearch/search", JSON.stringify({identifier: "apa", inputRows: []})).success(function(response) {
            console.log(response);
            return response;
        }).error(function(data, status, headers, config) {
                console.log(status, headers, config);
        });
    }

Take note of the payload (Yes, it's static for this example): JSON.stringify({identifier: "apa", inputRows: []})

This maps perfectly fine in my DTO. The fields are correct, and variable inputRows is an empty, initialized array.

HOWEVER when I look at the payload (Using firebug or the chrome inspector) , sent by the client, to the API, it looks like this: {"identifier":"apa","inputRows":"[]"}

Did you notice the "" around my JSON array?

{"identifier":"apa","inputRows":"[]"}
                                ^  ^
                                |  |

Yeah... That's the problem I'm having. When I send that request, the server simply replies with a HTTP 400, malformed syntax (Tomcat/Spring error page), the request doesn't even invoke the APIController on the back-end.

So, what I've tried so far (without success):

  • I have tried without using JSON.stringify() on the payload, with the same result
  • I have tried specifying the headers in my APIController (No use, it doesn't even execute that code before throwing the error)
  • I have tried using a Java.util.List instead of an array in the DTO

So I'm pretty much stuck here. Any help would be appreciated.

5
  • 2
    As I suspected there may be interesting behavior of stringify: stackoverflow.com/questions/710586/json-stringify-bizarreness Commented Sep 11, 2013 at 12:29
  • You can pass in the data as object, angularjs would serialize it itself. Try the method without using json.stringify Commented Sep 11, 2013 at 12:35
  • Please read the question again, I have stated that I have tried without JSON.stringify, but I still get the same result. @Chandermani Commented Sep 11, 2013 at 12:37
  • If i try to do JSON.stringify({test:[]}) on browser console i get,"{"test":[]}" Commented Sep 11, 2013 at 12:40
  • Bare in mind that we're using AngularJS, and that it gives me the same result when I don't use JSON.stringify at all. I've tried using both Firefox, and Chrome (latest versions, ECMAscript 5). @Chandermani Commented Sep 11, 2013 at 12:43

1 Answer 1

2

It sounds like you have a conflicting prototype.js dependency overriding some functions.

I would try

delete Array.prototype.toJSON

before posting your object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that worked! I hadn't checked if the old prototype.js was still included... It was. Thought it wasn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.