0

Hi I have the following code

In JQuery:

this.myFunction = function()
{

   var dto =
   {
       id : getID()
   };

   //alert(getID();) to verify that my number is indeed non zero.

    $.ajax({
        type: "POST",
        url: "Create",
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(dto),
        dataType: "json",
        success: function(result) {
            alert("Data Returned: ");
        }
    });
}

in C#

public void myCFunction(int i)
{
  //do some stuff
}

When I ran the debugger in Visual Studios, I noticed that the integer in my C# function is always zero despite that the value I pass it is not zero.

8
  • JSON.stringify(dto), should maybe be JSON.stringify(dataPost)? Commented Nov 14, 2012 at 22:07
  • Yeah that was my typo. Let me fix that in the post Commented Nov 14, 2012 at 22:08
  • 1
    JSON.stringify(dto) translates dto to {"id":12345}. Commented Nov 14, 2012 at 22:11
  • 2
    Have you tried changing your method signature (C#) to 'public void myCFunction(int id)', that way your JSON matches data within the function that will be processing it? Commented Nov 14, 2012 at 22:13
  • 1
    @layoric, yup that's the solution. Thanks! Commented Nov 14, 2012 at 22:16

1 Answer 1

1

Answer in comments, but this is to make it clearer.

Your JSON data needs to match the data that the C# method that is processing the request. 'i' is always 0 because no data is being processing by the method matches 'i'. Eg, it's being passed null data. Changing the data to match the parameter names will fix this problem. Eg,

public void myCFunction(int id)
{
  //do some stuff
}
Sign up to request clarification or add additional context in comments.

Comments

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.