C# I';我缺少一个Ajax参数,但我';我通过它&;浏览器正在确认这一点
简而言之,我因为没有传递参数而得到一个错误,但我正在传递它。下面是我试图做的&代码 我的jQuery AjaxC# I';我缺少一个Ajax参数,但我';我通过它&;浏览器正在确认这一点,c#,jquery,ajax,C#,Jquery,Ajax,简而言之,我因为没有传递参数而得到一个错误,但我正在传递它。下面是我试图做的&代码 我的jQuery Ajax var id = this.id; var viewd = JSON.stringify({ 'company': id, 'seen':true, 'viewCount':0, 'email':false }); $.ajax({ type: 'POST', u
var id = this.id;
var viewd = JSON.stringify({
'company': id,
'seen':true,
'viewCount':0,
'email':false
});
$.ajax({
type: 'POST',
url: '/WebServices/usageAjax.asmx/addView',
data: viewd,
dataType: "json"
});
它正在调用的代码
[WebMethod]
public void addView(string company, bool seen, int viewCount, bool email)
{
userManager um = new userManager();
string user = um.id;
um.addUsage(user, company, seen, 0, email);
}
将返回此错误:
System.InvalidOperationException:缺少参数:公司。在
System.Web.Services.Protocols.ValueCollectionParameterReader.Read(名称ValueCollection
(收集)在
System.Web.Services.Protocols.UrlParameterReader.Read(HttpRequest
(请求)在
System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()在 System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() 尽管chrome明确表示正在传递一个值:
添加
contentType:'application/json;charset=utf-8'
到您的Ajax请求:
$.ajax({
type: 'POST',
url: '/WebServices/usageAjax.asmx/addView',
data: viewd,
dataType: "json",
contentType: 'application/json; charset=utf-8' // <= Add this
});
$.ajax({
键入:“POST”,
url:“/WebServices/usageAjax.asmx/addView”,
数据:viewd,
数据类型:“json”,
contentType:'application/json;charset=utf-8'//您不需要将数据作为stringify方法传递。只需将数据作为formData传递即可。