1

I have a problem with this.
below are my jquery code and aspx.cs code.
I should send $('#UserID').val() to WebMethod as a parameter.
but in WebMethod it is always null.
if I change $('#UserID').val() to any string, it works well.
#UserID is correct. because I made a alert window with this value.
and.. I already changed data: '{userId: "' + $('#UserID').val() + '"}',
to data: JSON.stringify({ userId: $('#UserID').val() }).
but nothing works.

 $.ajax({
    url: "register.aspx/IsIdAvailable",
    type: "post",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: '{userId: "' + $('#UserID').val() + '"}',
    dataFilter: function (data) {
        var msg = JSON.parse(data);
        if (msg.hasOwnProperty('d'))
            return msg.d;
        else
            return msg;
    }
})

[WebMethod]
public static bool test(string userId)
{
    System.Diagnostics.Trace.WriteLine("userId: " + userId +"!");
    ...
}

output is always userId: !

6
  • apparently $('#UserID').val() is null. Can you try this: alert($('#UserID').val()); ? Commented Apr 15, 2015 at 18:12
  • Watch the network request in your browser debug console to see how it is posted to your server-side code. Commented Apr 15, 2015 at 18:12
  • 1
    You don't need the single quote. Change it to: data: {userId : $('#UserID').val() }, Commented Apr 15, 2015 at 18:15
  • I did everything you guys said. but it doesn't work. but thank you all. Commented Apr 15, 2015 at 18:38
  • You wrote register.aspx/IsIdAvailable in the script but test in C#. Is it a mistake in copy paste here? Commented Apr 15, 2015 at 18:59

1 Answer 1

1

Try changing your data attribute to something like:

data: {'userId': $("#UserID").val()}
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.