我试图通过AJAX将一些数据发布到VB.NET WebMethod。
JSON.stringify(myRows)
包含:
{
"myRows":[
{
"UniqueId":"188",
"Description":"hello",
"ClientId":"321",
"SecretKey":"dftete",
"Active":"checked",
"Delete":"delete icon"
},
{
"UniqueId":"191",
"Description":"sfsss",
"ClientId":"fsdfs",
"SecretKey":"cvvcvb",
"Active":"unchecked",
"Delete":"delete icon"
},
{
"UniqueId":"201",
"Description":"I am test singh",
"ClientId":"23424242",
"SecretKey":";kfddgdfl;ghf",
"Active":"unchecked",
"Delete":"delete icon"
},
{
"UniqueId":"202",
"Description":"Yay mai ban ne wala hun",
"ClientId":"n.csdvnsssl",
"SecretKey":"nj.ssdnfvel,vgd",
"Active":"unchecked",
"Delete":"delete icon"
}
]
}
我的AJAX电话是:
$.ajax({
type: "POST",
url: "MyWebServiceUtilities.asmx/savesocialloginkeys",
data: JSON.stringify(myRows),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
//some code here
},
failure: function (response) {
//some code here
},
error: function (response) {
//some code here
}
});
服务器端网络方法是这样的:
<WebMethod()> _
Public Function savesocialloginkeys(ByVal myrows As String) As String
Dim response As String = ""
'------------Some code here-------------------------------
'------------Response will be based on results as per code-------
Return response
End Function
当我尝试调试时,AJAX调用显示错误!
答案 0 :(得分:0)
AJAX调用失败,因为您正在向WebMethod发送JSON对象,但WebMethod接受String。 ASP.NET试图变得聪明并将字符串化的JSON对象转换为真实对象,因此当请求到达WebMethod时,它不再是字符串。
您需要一个代表JSON结构的简单对象:
Public Class SocialConnectionModel
Public Property UniqueId As String
Public Property Description As String
Public Property ClientId As String
Public Property SecretKey As String
Public Property Active As String
Public Property Delete As String
End Class
由于您的JSON对象包含myRows
项下的对象数组,因此WebMethod签名必须接受:
Imports System.Web.Services
Imports System.ComponentModel
<System.Web.Script.Services.ScriptService()>
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class MyWebServiceUtilities
Inherits System.Web.Services.WebService
<WebMethod()>
Public Function savesocialloginkeys(myRows As SocialConnectionModel()) As Boolean
' Do something with the data
' Return true if succeeded
Return True
End Function
End Class
如果您还没有这样做,包括<ScriptService()>
行很重要,因为您从AJAX调用此WebMethod。
现在你的AJAX调用应该按预期工作:
$.ajax({
type: "POST",
url: "MyWebServiceUtilities.asmx/savesocialloginkeys",
data: JSON.stringify(myrows),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (response && response.d) {
console.log("success");
}
},
failure: function (response) {
// some code here
},
error: function (response) {
// some code here
}
});
建议:尝试使用适当的类型构建JSON数据,而不是为每个属性构建字符串。例如,UniqueId属性应该是一个整数,而Active属性可以是一个bool(我猜测)。除非必须,否则不要做所有事情stringly typed。 :)
答案 1 :(得分:0)
任何仍在这里落脚的人,这就是解决方法:
<WebMethod()> _
Public Function savesocialloginkeys(ByVal myRows As Object) As String
'------------Now you can work on myRows with the help of newtonsoft library-----'
End Function
答案 2 :(得分:-1)
您没有向服务器发送json对象,因此您必须使用'text / html'作为contentType,如:
$.ajax({
type: "POST",
url: "MyWebServiceUtilities.asmx/savesocialloginkeys",
data: JSON.stringify(myRows),
contentType: "text/html; charset=utf-8",
dataType: "json",
success: function (response) {
//some code here
},
failure: function (response) {
//some code here
},
error: function (response) {
//some code here
}
});