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.

3
  • are you ever calling LoadObject() ? Commented May 3, 2011 at 18:07
  • Yes, it's done when the user clicks on a div $('#thediv').click(function () {LoadObject();}); Commented May 3, 2011 at 18:12
  • When I add the line alert(TheObject); I get "[Object] [Object]" Commented May 3, 2011 at 18:18

3 Answers 3

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

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

6 Comments

ok, cool. Can I remove the methodParameterName and leave just the parameters as they are?
That should work, too. In my example, the methodParameterName argument of the GetCount method accepts a complex type.
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?
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).
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.
|
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

5 Comments

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.
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
What browser are you using?, could you add the code of your method on the code behind?
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.
the errors that you are getting must be related to any other parse of your code, maybe this link could be helpful. stackoverflow.com/questions/3950602/…
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"
});

4 Comments

I'm using the library suggested in the answer above but I get an error. Can you see why? Thanks.
I think your problem is the path. Make sure "../Pages/TestPage.aspx/GetCount" the right path regardind your JS (not regarding your current page).
The path is fine, I have another page method in another file and the path is of the same type.
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.

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.