我已经阅读了其他一些问题,但仍无法找到我的具体问题的答案。我试图用Ajax Post调用vb.net sub。 ajax函数正在取得成功,但从未在我的sub中遇到断点或实际插入到我的数据库中。任何帮助表示赞赏。
aspx.vb
<System.Web.Services.WebMethod()> _
Public Shared Sub insertKPI(id As String)
Dim constr As String = ConfigurationManager.ConnectionStrings("SqlServerConnectionString").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand
cmd.CommandText = "kpi_InsertKPI"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("PartnerLinkCode", HttpContext.Current.Session.Contents("partnerlinkcode").ToString)
cmd.Parameters.AddWithValue("UserLinkCode", HttpContext.Current.Session.Contents("userlinkcode").ToString)
cmd.Parameters.AddWithValue("KPIInfoLinkCode", id)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
脚本
function addWidget(id){
$.ajax({
type: "POST",
url: "../secure/scorecardtable.aspx/insertKPI",
data: '{"id":' + JSON.stringify(id) + '}',
datatype: "json",
success: function(result){
alert("New kpi should be in database");
console.log(result);
},
error: function(xhr, textStatus, errorThrown){
alert("There was an error inserting the kpi into the database. " + xhr.status + ': ' + errorThrown);
}
}); // End Ajax
}
答案 0 :(得分:0)
将contentType
添加到我的ajax函数实际上使它工作。
function addWidget(id){
$.ajax({
type: "POST",
url: "../secure/scorecardtable.aspx/insertKPI",
data: '{"id":' + JSON.stringify(id) + '}',
contentType: "application/json; charset=utf-8",
datatype: "json",
success: function(result){
alert("New kpi should be in database");
console.log(result);
},
error: function(xhr, textStatus, errorThrown){
alert("There was an error inserting the kpi into the database. " + xhr.status + ': ' + errorThrown);
}
}); // End Ajax
}