筆記一下 jQuery AJAX 傳遞 JSON 到 ASP.Net 的方式
拿工作上的例子來 demo 好了
=======================================
SuncolorFBLogin.aspx
javascript :
FB.api('/me', function (response) {
var member = {member: {
"uid": response.id,
"cname": response.name,
"email": response.email,
"birthday": response.birthday,
"gender": response.gender
}};
$.ajax({
url: "SuncolorFBLogin.aspx/FBMemberCheck",
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(member),
dateType: "json",
success: function (res) {
document.write(res.d.uid);
}, error: function (err) {
alert(err.responseText);
}
});
});
SuncolorFBLogin.aspx.vb
ASP.Net (因為是老專案所以用VB.Net,用C#自己轉應該也還好)
Imports System.Web.Services
Imports System.Web.Script.Services
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System.Web.Script.Serialization
<WebMethod()> _
Public Shared Function FBMemberCheck(ByVal member As member) As member
'可以直接 member.uid 取值
'如果要還原成JSON字串就用下面這樣
Dim objSerializer As JavaScriptSerializer = New JavaScriptSerializer()
Dim json As String = objSerializer.Serialize(member)
'如果要把JSON字串反序列化再傳出去就用下面這樣
Dim obj As JavaScriptSerializer = New JavaScriptSerializer()
Return obj.Deserialize(Of member)(json)
End Function
Public Class member
Dim _uid As String = ""
Dim _cname As String = ""
Dim _email As String = ""
Dim _birthday As String = ""
Dim _gender As String = ""
Public Property uid() As String
Get
Return _uid
End Get
Set(ByVal value As String)
_uid = value
End Set
End Property
Public Property cname() As String
Get
Return _cname
End Get
Set(ByVal value As String)
_cname = value
End Set
End Property
Public Property email() As String
Get
Return _email
End Get
Set(ByVal value As String)
_email = value
End Set
End Property
Public Property birthday() As String
Get
Return _birthday
End Get
Set(ByVal value As String)
_birthday = value
End Set
End Property
Public Property gender() As String
Get
Return _gender
End Get
Set(ByVal value As String)
_gender = value
End Set
End Property
End Class
留言列表