我有以下HTML超链接,我想在它单击时在jQuery中调用
<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>
当我单击超链接时,将执行以下代码
<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>
然后在Web服务文件中,我编写了以下C#代码,这些代码具有我从上面的jQuery调用的Web方法的定义。
[WebMethod]
public void updateNotRead(string userID)
{
u.updateNotReadStatus(userID);
}
当我调用Web服务时,我在浏览器中收到以下消息
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()
那我怎么给web方法加上userID参数呢?
答案 0 :(得分:2)
尝试这样:
var uID = '<%=Session["userID"].ToString() %>';
$.ajax({
url: 'TaskService.asmx/updateNotRead',
data: "{userID : '" + uID + "'}",
type: 'POST',
contentType: 'application/json',
dataType: 'json'
});
答案 1 :(得分:1)
我认为您的问题出在数据参数上。
data:{userID:uID},
代替
data: '{userID: "' + uID + '"}',
我将使用对象para
来包含参数,这可以使代码更简单。
var uID = '<%=Session["userID"].ToString() %>';
var para = {userID:uID};
$.ajax({
url: 'TaskService.asmx/updateNotRead',
data: para,
type: 'POST',
contentType: 'application/json',
dataType: 'json'
});
答案 2 :(得分:1)
data: '{userID: "' + uID + '"}',
应该是:
data: "{userID : '" + uID + "'}",