views:

240

answers:

3

I have in my javascript these 2 functions "classes":

// product class
function Product() {
 this.id;
 this.qty;
 this.size;
 this.option;
}

// room class
function Room() {
 this.id;
 this.type;
 this.products = [];
}

I have my js logic which fills rooms and their products.

Now i want to send array of rooms to a webservice to do some calculations and get back from it the result.

How to send this array objects to the service and whats the data type which the service will receive to loop through and process?

I tried to write the javascript code like this:

  $.ajax({
   type: "POST",
   contentType: "application/json; charset=utf-8",
   url: "_Services/MyWebService.asmx/CalculatePrices",
   data: "{'rooms':'" + roomsObjects + "'}",
   dataType: "json",
   success: function(result) {
    alert(result.d);
   }
  });

And the webservice like this:

[WebMethod]
    public string CalculatePrices(object rooms)
    {
     return "blabla";
    }

but i find that rooms in the wbservice is always = [object Object]

A: 

Change:

data: "{'rooms':'" + roomsObjects + "'}",

to:

data: {'rooms':roomsObjects},
w35l3y
gave me a big error after changing "Invalid JSON primitive: rooms....."
Amr ElGarhy
w35l3y
The data parameter must be quoted when calling ASP.NET AJAX services. If you pass jQuery an object as the data parameter, it will serialize it as k=v pairs in the POST request. ASP.NET AJAX services expect a JSON string representing the parameters instead, not k=v pairs.
Dave Ward
+3  A: 

For that case this would work:

//...
   data : '{"rooms":[' + roomsObjects.join() + ']}',
//...

The above code will generate a valid JSON string, but I recommend you to get a JSON library and use JSON.stringify function:

      $.ajax({
              type: "POST",
              contentType: "application/json; charset=utf-8",
              url: "_Services/MyWebService.asmx/CalculatePrices",
              data: JSON.stringify({'rooms': roomsObjects}),
              dataType: "json",
              success: function(result) {
                 alert(result.d);
              }
      });
CMS
This great, gave the me exactly what i want as i now have the rooms object on the service and can can find the products inside, thank you and for the creator of this library
Amr ElGarhy
+2  A: 

Hey, Amr.

If you don't mind including a tiny JavaScript library, I think using json2.js' JSON.Stringify is the best way to serialize objects for use with ASP.NET AJAX services.

Here's a snippet from that post:

// Initialize the object, before adding data to it.
//  { } is declarative shorthand for new Object().
var NewPerson = { };

NewPerson.FirstName = $("#FirstName").val();
NewPerson.LastName = $("#LastName").val();
NewPerson.Address = $("#Address").val();
NewPerson.City = $("#City").val();
NewPerson.State = $("#State").val();
NewPerson.Zip = $("#Zip").val();

// Create a data transfer object (DTO) with the proper structure.
var DTO = { 'NewPerson' : NewPerson };

$.ajax({
  type: "POST",
  contentType: "application/json; charset=utf-8",
  url: "PersonService.asmx/AddPerson",
  data: JSON.stringify(DTO),
  dataType: "json"
});

There's no array in that example, but JSON.Stringify does serialize JavaScript arrays in the correct format to send in to ASP.NET AJAX services for array and List parameters.

A nice thing about using JSON.Stringify is that in browser that support native JSON serializing (FF 3.5, IE 8, nightly builds of Safari and Chrome), it will automatically take advantage of the browser-native routines instead of using JavaScript. So, it gets an automatic speed boost in those browsers.

Dave Ward