开发者

Why don't I get the json string /command object back when using jQuery ajax post method?

I am using Spring 2.5.5, jQuery 1.4.4, spring-json1.3.1 (and the optional sojo-optional-0.5.0.jar - it says optional, but I needed this cause it threw some ClassNotFound exception regarding sojo - but this is not the issue :) ).

I am interested why when using POST in contrast to GET with the following code:

$.ajax({
      url: "sizesOrColors.json",
      global: false,
      type: "POST",//"GET",
      data: "productId=1&sizeOrColorId=111&isSize=true",
      dataType: "json",//htm开发者_如何学Gol",
      async:false,
      success: function(javascriptObjectFromJsonFile){
      var colorsOrSizesMap = javascriptObjectFromJsonFile.command.colorsOrSizesMap;
      },
      error: function(XMLHttpRequest, textStatus, errorThrown){
      //some error handling code...
      }
   }
);

the javascriptObjectFromJsonFile doesn't have the "command" property? When using the GET method, I receive the "command" property.

When accessing the success callback function, with the POST method, the console throws:

Uncaught TypeError: Cannot read property 'colorsOrSizesMap' of undefined

Remember: If specifying dataType: "json", jQuery converts the json string to a javascript Object:Handling the JSON data type. (Previously I tried with parseJSON but now I know that's not appropriate).

Example of the json string I receive with the GET method is:

{"command":{"colorsOrSizesMap":{"123":"Blue","128":"White"},"errorsFromGet":[]}}

Now, I suspect there is something to do with the way the Spring's SimpleFormController.onSubmit method handles the POST method (as described by spring-json1.3.1)

Even the type converting of CommandBean-Properties is supported.

The GET request is hitting the formBackingObject method.

The POST request is hitting the onSubmitAction method.

Although here spring-json team says the POST request is hitting the onSubmitAction method, I never got it to go there. And the processing path goes through the formBackingObject also. The correct path the Controller took was:

-formBackingObject(HttpServletRequest request)

-initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception

-Validator.validate(...) method

-onSubmit(Object command, BindException errors) throws Exception

(not onSubmitAction)

This is regardless using sessionForm true or false (in the servlet configuration when configuring the SimpleFormController json bean).

There is no problem when I receive the command object in the onSubmit method - it is populated with all the parameters from the data string ("productId=1&sizeOrColorId=111&isSize=true"). But when the method returns the ModelAndView, something happens either on Spring side (but there is no exception or error), or on the client side when jQuery tries to convert the json string to javascript object. Probably, since I don't get the "command" property in the javascript object with the POST method (and get it with the GET method), there is some problem on the server side. But what?

If you can't get the solution from the case I presented above, I would be glad to post some additional code on your demand.

Any help would be appreciated. Particularly form someone that already implemented the spring-json library.

Kind Regards,

Despot

P.S.: As you can see, I avoided using the prototype.js library (evalJSON() method and Ajax object both are part of prototype - see the JavaScript behaviour-roles.js)

EDIT: This is how the javascriptObjectFromJsonFile object looks like (in the image the javascriptObjectFromJsonFile is msg):

Why don't I get the json string /command object back when using jQuery ajax post method?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜