0

I asked a question yesterday where it was closed with duplication reason, However I still did not get an answer how to resolve my issue and i need some assistance.

I am using ASP.NET with script manager

<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" EnablePartialRendering="true" runat="server" />

I am getting error 500 when trying to post data to server.

Bad code with Error 500:

CS:

[WebMethod]
public static void SetCurrentBaseVersion(string id)
{
    // need to get here
}

JS:

function postNewBaseLine() {
    var id = "300";

    $.ajax({
        type: "POST",
        url: "ManagerBaseKit.aspx/SetCurrentBaseVersion",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: {'id' :id},
        success: function (data) {
            alert('success!');
        },
        statusCode: {
            500: function () {
                alert('got error 500');
            }
        }
    });
}

What I found so far that if I remove the string id in the webmethod it is working fine and I am being able to reach the SetCurrentBaseVersion (not getting error 500)

Working Code:

CS

[WebMethod]
public static void SetCurrentBaseVersion() //removed the string id
{
    // need to get here
}

JS

function postNewBaseLine(id) {
    var id = "300";

    $.ajax({
        type: "POST",
        url: "ManagerBaseKit.aspx/SetCurrentBaseVersion",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        //data: {'id' :id},   removed the data
        success: function (data) {
            alert('success!');
        },
        statusCode: {
            500: function () {
                alert('got error 500');
            }
        }
    });
}
user829174
  • 6,132
  • 24
  • 75
  • 125

1 Answers1

0

You have to pass data as a json string you need to put the value of data in double quotes to make it string. The json format has key value pair within quotes. You need to understand Json Data Format

  data: "{'id': '" + id+ "'}",

This also worked without using quotes around key and value but json document which I referred states for putting quotes around key and value.

  data: "{id: " + id + "}",

For method having two parameters,

Code behind

[WebMethod()]
public static string CallMethodFromClient(string id, int amount)
{ 

HTML

    data: "{ id : " + id + ", amount : " + amount + " }"
Adil
  • 146,340
  • 25
  • 209
  • 204