2

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]

2
  • does the webservice already exist? is it expecting a certain format? Commented Sep 9, 2009 at 0:16
  • Yes it exist and it receive data "wrong data" Commented Sep 9, 2009 at 0:21

3 Answers 3

3

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);
              }
      });
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

Change:

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

to:

data: {'rooms':roomsObjects},

2 Comments

I don't know how roomsObjects is organized jquery docs says to use objects or strings like "var1=value1&var2=value2" docs.jquery.com/Ajax/jQuery.ajax#options
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.

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.