0

I have the following HTML hyperlink I want to call it in jQuery when it clicked

<a href="#" data-toggle="dropdown" id="notID" class="dropdown-toggle f-s-14">
                <i class="fa fa-bell"></i>
                <span runat="server" id="notCount" class="label">5</span>
            </a>

When i click the hyperlink the following code executed

<script src="assets/plugins/jquery/jquery-1.9.1.js"></script>
<script>
    $(document).ready(function () {
        $("#notID").click(function () {

            var uID = '<%=Session["userID"].ToString() %>'

            $.ajax({
                url: 'TaskService.asmx/updateNotRead',
                data: '{userID: "' + uID + '"}',
                type: 'POST',
                contentType: 'application/json',
                dataType: 'json'
            });
            return false;
        });
    });
</script>

And in the webservice file i wrote the following C# code that have the definition for the webmethod that i call from jQuery above.

[WebMethod]
    public void updateNotRead(string userID)
    {
        u.updateNotReadStatus(userID);
    }

When i call the web service i got the following message in the browser

System.InvalidOperationException: Missing parameter: userID.
   at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
   at System.Web.Services.Protocols.UrlParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

So how could I give the web method the userID parameter?

3 Answers 3

2
data: '{userID: "' + uID + '"}',

It should be:

data: "{userID : '" + uID + "'}",
2

Try like this:

var uID = '<%=Session["userID"].ToString() %>';
$.ajax({
    url: 'TaskService.asmx/updateNotRead',
    data: "{userID : '" + uID + "'}",
    type: 'POST',
    contentType: 'application/json',
    dataType: 'json'
});
1

I think your problem is on data parameter.

data:{userID:uID},

instead of

data: '{userID: "' + uID + '"}',

I would use an object para to contain parameter, which can let the code simpler.

var uID = '<%=Session["userID"].ToString() %>';
var para = {userID:uID};
$.ajax({
    url: 'TaskService.asmx/updateNotRead',
    data: para,
    type: 'POST',
    contentType: 'application/json',
    dataType: 'json'
});

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.