将JSON数据发送到WebMethod
我试图通过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
}
});
服务器端Web方法是这样的:
<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调用显示错误!
AJAX调用失败,因为您正在向WebMethod发送JSON对象,但WebMethod接受字符串。 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属性可以是一个布尔(我猜)。 除非必须,否则不要将所有内容拼写完成。 :)
你不发送一个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
}
});
链接地址: http://www.djcxy.com/p/46157.html