0

I'm tring to pass a value I pull from a data-attribute in my markup to a C# method using jQuery Ajax. In this example, the value for QuestionNumber results in a 1. EMHQQuestion is an enum with values 1 through 15. I expect my C# method DeleteCondition to receive that 1 but instead I get a 500 internal server error: "Invalid web service call, missing value for parameter: 'questionNumber'.

Any suggestions?

function DeleteConditions() {
    var QuestionNumber = $('.noRadioButton').data('questionnumber');
    $.ajax({
        url: "mhqpreinterview.aspx/deletecondition",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        questionNumber: QuestionNumber
    });
    Dialog.dialog('close');
}

..

[WebMethod(EnableSession = true)]
public static void DeleteCondition(EMHQQuestion questionNumber)
{
    //stuff
}

2 Answers 2

3

I struggled with this EXACT same thing when making AJAX reqs to web form methods.

For your c# method:

[System.Web.Services.WebMethod()]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string DeleteCondition(EMHQQuestion questionNumber)
{
  // do enum stuff here         
}

Please note that I have changed the method type from void to string. It is a good idea to send some identifiable information back to the client. Even if you do not have to send data back, it gives you a chance to customize success or helpful debugging information.

Here are the changes you have to make to your AJAX object:

var params = '{ questionNumber: ' + JSON.stringify(QuestionNumber) + '}';
var post   = {
          type: 'POST',
          url: 'mhqpreinterview.aspx/deletecondition',
          data: params,
          contentType: "application/json; charset=utf-8",
          dataType: "json"
};

$.ajax(post);

What you should be taking away from the above javascript, is the use of JSON.stringify before you send to your method. You have to make sure QuestionNumber is parameterized correctly and already valid JSON for the web service method to receive.

If you find its still not working, check to see what value is being stored inside QuestionNumber before you are trying to send it.

The above code does work with me, any other troubles post a comment and I will do my best to help answer it.

0
1

Use data jquery ajax field:

    data: JSON.stringify({QuestionNumber: QuestionNumber}),
    dataType: "json",
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.