Rhs
Rhs

Reputation: 3318

AJAX Incorrect Parameter

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.

Upvotes: 0

Views: 91

Answers (1)

Darren Reid
Darren Reid

Reputation: 2322

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
}

Upvotes: 1

Related Questions