1

This is probably a very simple task to perform but I'm taking the risk to ask anyway.

I have an object variable that looks like this:

var MyObj = {"Param1": "Default",
             "Param2": "test",
             "Param3": 3 };

I'm using ASP.net and I'm looking to pass this object to a page method via jquery.

So far, I have this javascript code:

function LoadObject () {

  var TheObject = MyObj.toString();

  $.ajax({
    type: "POST",
    url: "../Pages/TestPage.aspx/GetCount",
    data: TheObject,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: successFn,
    error: errorFn
    });
};

I have a page method set up in the .cs file and I put a breakpoint in it but it never gets there; nothing happens.

Please let me know what changes I need to make to get this to work. Thanks.

Erick Petrucelli
  • 14,386
  • 8
  • 64
  • 84
frenchie
  • 51,731
  • 109
  • 304
  • 510

3 Answers3

2

You need to serialize TheObject into a JSON string, and ensure that the GetCount method accepts an object with the same signature as TheObject.

I use the jQuery.JSON library to do this so that my syntax becomes:

data: "{ methodParameterName: " + $.toJSON(TheObject) + " }"

I use this library, but you can acheive the same thing with any other library in a similar manner

BradBrening
  • 5,418
  • 25
  • 30
  • ok, cool. Can I remove the methodParameterName and leave just the parameters as they are? – frenchie May 03 '11 at 18:43
  • That should work, too. In my example, the methodParameterName argument of the GetCount method accepts a complex type. – BradBrening May 03 '11 at 18:46
  • I get a 500 error. In the .cs file, I have [WebMethod] public static string GetCount(string TheObject) { string test="test"; return test;} . What's missing? – frenchie May 03 '11 at 19:06
  • Using your example, your method needs to take in the parameters you've specified: string Param1, string Param2, string Param3 (or whatever data type those params are). – BradBrening May 03 '11 at 19:47
  • Alternatively, you can create a struct in your C# codebehind with the same signature as the JS object, then accept a parameter of that type in GetCount. This is what I envisioned in my initial answer. – BradBrening May 03 '11 at 19:53
  • For now I have another problem: when I add the json.js file, I get a bunch of javascript error even before I do anything with the page method. These are the errors: Uncaught TypeError: Cannot read property 'enumValueNotInteger' of undefined ScriptResource.axd:357Uncaught TypeError: Cannot read property 'argumentUndefined' of undefined ScriptResource.axd:1068Uncaught TypeError: Cannot call method 'resolveElement' of undefined ScriptResource.axd:483Uncaught TypeError: undefined is not a function – frenchie May 03 '11 at 19:54
0

Take a look at this thread: JSON stringify missing from jQuery 1.4.1?

Abstract: jQuery doesn't have a native method to do it. But there are many plugins out there.

EDIT

Sample C# code receiving your JSON object:

[WebMethod]
public static int GetCount(GetCountParams p)
{
    // ... Do something with p.Param1, p.Param2, etc.
    return 0;
}

public class GetCountParams
{
    public string Param1 { get; set; }
    public string Param2 { get; set; }
    public string Param3 { get; set; }
}

EDIT 2

Sample jQuery AJAX call using that object as parameter:

$.ajax({
    type: "POST",
    url: "../Pages/TestPage.aspx/GetCount",
    data: "{ p: '" JSON.stringify(MyObj) + "' }",
    contentType: "application/json; charset=utf-8",
    dataType: "json"
});
Community
  • 1
  • 1
Erick Petrucelli
  • 14,386
  • 8
  • 64
  • 84
  • I'm using the library suggested in the answer above but I get an error. Can you see why? Thanks. – frenchie May 03 '11 at 19:09
  • I think your problem is the path. Make sure "../Pages/TestPage.aspx/GetCount" the right path regardind your JS (not regarding your current page). – Erick Petrucelli May 03 '11 at 19:18
  • The path is fine, I have another page method in another file and the path is of the same type. – frenchie May 03 '11 at 19:55
  • 1
    Now I saw that your parameter on C# is of type String. This isn't correct. Or you creates a `class` with the structure you want (the same structure of the JS object) or you can use `object` as the type. Using object as type will also cause problems to read properties (using Reflection), then I really suggest creating a class. – Erick Petrucelli May 04 '11 at 00:01
0

The first thing that you need to know is that you need to match your method name with your url

for example if your method on your code behind is named "calculate", your url must be something like this "../Pages/TestPage.aspx/calculate"

other thing that you need to keep in mind is the parameters of your method, the names and the types of your parameters must match in you ajax call and your method (code behind)

if the sign of your method is something like this

[WebMethod] public void Calculate(string data){
// your code here
}

Your ajax call must be like this:

function LoadObject () {
     var objetoJson = {
         data: JSON.stringify(MyObj)
     };

    $.ajax({
          type: "POST",
           url: "../Pages/TestPage.aspx/Calculate",
           data: objetoJson ,
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: successFn,
           error: errorFn
           });
};

This section is so important:

     var objetoJson = {
         data: JSON.stringify(MyObj)
     };

the name "data" is the name of your parameter in your method (code behind) and "JSON.stringify" is a helper functions already defined on your browser to convert and object to string

Hope this helps

Edgar
  • 71
  • 3
  • ok, the line you mentioned is fixed. Now, I just realized that when I add the json library, it's creating a few javascript errors that with the ScriptResource.axd file. Any idea why? Thanks. – frenchie May 03 '11 at 19:38
  • These are the errors: Uncaught TypeError: Cannot read property 'enumValueNotInteger' of undefined ScriptResource.axd:357Uncaught TypeError: Cannot read property 'argumentUndefined' of undefined ScriptResource.axd:1068Uncaught TypeError: Cannot call method 'resolveElement' of undefined ScriptResource.axd:483Uncaught TypeError: undefined is not a function – frenchie May 03 '11 at 19:42
  • What browser are you using?, could you add the code of your method on the code behind? – Edgar May 03 '11 at 19:50
  • I'm using chrome. The .cs file is really simple for now: [WebMethod] public static string GetCount(string TheObject) { string test="test"; return test;} . Note that the errors mentioned above occur even before I start doing anything with the page method. – frenchie May 03 '11 at 19:52
  • the errors that you are getting must be related to any other parse of your code, maybe this link could be helpful. http://stackoverflow.com/questions/3950602/casting-enum-value-to-int-when-binding-to-dropdown-using-reflection – Edgar May 03 '11 at 20:01