4

I have a problem in passing a JavaScript object to webmethod in asp.net.

The JavaScript object is:

var Materials = new Object();
function() {
            Materials.MaterialName = $('[id*="txtMaterialName"]').val();
            Materials.QuantityType = $('[id*="txtquantity"]').val();               
            AddNewMaterialToDb(Materials);
            $(this).dialog('close');
        }

Here materials is the object and now I want to pass it to a web method which takes a parameter of class type.

Now I have two option:

  1. either to define the webmethod to take a parameter of MaterialEntity class which would automatically understand the JSON string passed from the AJAX method
  2. to create the webmethod to take the JSON string and serialize into MaterialEntity class

How to do that when I am using jQuery AJAX?

I mean to be specific how should I pass the jQuery object as data for jQuery AJAX so that any of the above two conditions gets satisfied?

Function for jQuery AJAX:

function AddNewMaterialToDb(materials) {

$.ajax({
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    url: 'Services/Service.asmx/AddNewMaterial',      
    data :'{"Materials":"' + JSON.stringify(materials).replace('"', '\\\"') + '"}',
    dataType: "json",
    success: function(data, textStatus) {
        if (textStatus == "success") {
            if (data.d == true) {
                alert('New Item Added');
            }
        }
    },
    error: function(data, textStatus) {
        alert('An error has occured retrieving data!');
    }
});
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Joy
  • 6,438
  • 8
  • 44
  • 75
  • perhaps you cannot pass JS object. – S L Apr 17 '11 at 04:58
  • @experimentX i know that ... i meant actually if i can convert it into a json format by JSON.stringify method . and then pass it .. but somehow i am getting error . i have already used that in the code please check it – Joy Apr 17 '11 at 05:03
  • Take a look at the link I have edited into my answer below – Hari Pachuveetil Apr 18 '11 at 12:56

2 Answers2

2

Please see if this answer helps. Look especially at all the attributes that the service class and service method has.

EDIT: This article has some tips this question could use

Community
  • 1
  • 1
Hari Pachuveetil
  • 10,294
  • 3
  • 45
  • 68
  • yes thats a really good link... thanks for that . i would use it for future reference . and i hav already solved the problem. :) – Joy Apr 20 '11 at 03:53
-1

you can do something like this:

$.ajax({
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    url: 'Services/Service.asmx/AddNewMaterial',      
    data : { name: materials.MaterialName, quantity: materials.QuantityType } ,
    success: function(data, textStatus) {
        if (textStatus == "success") {
            if (data.d == true) {
                alert('New Item Added');
            }
        }
    },
    error: function(data, textStatus) {
        alert('An error has occured retrieving data!');
    }
});

Now in your asp.net Web method you can simple use Request.Form["name"] to get the material name and Request.Form["quantity"] to get quantity type. This way your web method will become generic and you wouldn't have to pass any parameters to your web method.

Naveed Ahmad
  • 3,176
  • 1
  • 15
  • 18
  • the problem is i can always pass that . but the parameter should be either a single string or a custom class "MaterialEntity" in my webmethod . so i really cant do that cause that would obviously provide me with the constriant that i alwz have to declair multiple variables for the parameters . so thats not really a viable solution . and because of that reason only i needed to pass a json string or a json object as serialized which i could later on either take as custom object or deserialize the json string accepted as custom object – Joy Apr 17 '11 at 05:22