0

Can Anyone help me on this, i m trying to convert complex json object send through ajax into a object. so that i can use this object to pass into my model.

The JSP code is:

function callRemovefilter()
{
    var jsonObjects = {
        address1: "Address_1",
        city: "City",
        pin: "PIN"
        };
    var jsonObjects2 = {
        locality:"Loc1",
        shippingType:"Regular",
        shippingCost:20
    };

    var cust= JSON.stringify(jsonObjects);
    var sales=JSON.stringify(jsonObjects2);
    jQuery.ajax({
              url: "http://localhost:8080/OnlineStore/kmsg/grocery/SaveSalesOrder",
              type: "GET",
              data: {CustomerInfo:cust,SalesModel:sales},
              dataType: "json",
              beforeSend: function(x) {
                if (x && x.overrideMimeType) {
                  x.overrideMimeType("application/j-son;charset=UTF-8");
                }
              },
              success: function(result) {
             //Write your code here
              }
    });
}

// The controller code is

  @RequestMapping(value = "/SaveSalesOrder", method = RequestMethod.GET)
            @ResponseStatus(value=HttpStatus.OK)
            public @ResponseBody String SaveCustomerOrder(@RequestParam Map<String,String> requestParams) throws Exception 
            {       
                ObjectMapper objectMapper = new ObjectMapper();
                SalesCommandObject salesCommandObject= new SalesCommandObject();
                salesCommandObject = objectMapper.readValue(body, SalesCommandObject .class);


                return "Success";
            }

// Code of JSP to send object to controller

var salesCommandObject = {};                      salesCommandObject.CustomerInfo =
            {
            "address1": "Address_1",
            "city": "City",
            "pin": "PIN"
            };
salesCommandObject.SalesModel = 
            {
            "locality":'Loc1',
            "shippingType":'Regular',
            "shippingCost":20
            };

           $.ajax
           ({
              type: "POST",
              dataType : 'json',
              async : true,     
              url: "http://localhost:8080/OnlineStore/kmsg/grocery/SaveSalesOrder",
              data : JSON.stringify(salesCommandObject),
              }).done(function(data,type,xml)
                        {
                                                  console.log(data);
                        }).fail(function()
                                  {
                            alert("Something Bad Happened, Service failed");
                      })
1

1 Answer 1

0

Send objects, not jsonstrigs. And in controller in your method SaveCustomerOrder get an object, not Map, like:

@RequestMapping(value = "/SaveSalesOrder", method = RequestMethod.GET) @ResponseStatus(value=HttpStatus.OK) public @ResponseBody String SaveCustomerOrder(@RequestParam CustomerInfo ci, @RequestParam SalesModel sm) throws Exception {
//your logic here return "Success"; }
And add getters and setters to appropriate classes(i.e CustomerInfo, SalesModel) like:

`public class SalesModel{
    private String sale_id;//or whatever field or property you need
    public String getSale_Id() {
        return sale_id;
    }
    public void setSale_Id(String si) {
        this.sale_id = si;
    }

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

17 Comments

u r saying to send Json Object not string ? is there any problem, in the way i am sending the json data. am i not sending the json object , if not then how ? also when i try using my model objects as parameters to my controller method then error thrown by firebug is unsupported media type,
send javascript objects! if you set your getters and setters properly, it should work
please, more about exception
i m new to jquery, can u show me the sample for sending objects properly ?
var saveData = {}; saveData.one = "test"; saveData.two = "tes2"; $.ajax({ type: "POST", url: "MyService.aspx/GetDate", data: JSON.stringify(saveData), // NOTE CHANGE HERE contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { alert(msg.d); }, error: function(msg) { alert('error'); } });
|

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.