<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Ajax1.aspx.vb" Inherits="WebApp001.Ajax1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="jquery/jquery-1.11.2.min.js"></script>
<script>
$(document).ready(function () {
$("#send").click(function () {
$.ajax({
type: "P0ST",
url: "Ajax1.ashx",
data: JSON.stringify([
{ "name": "Tomo", "age": 43 },
{ "name": "Kazuki", "age": 11 },
{ "name": "Chika", "age": 7 },
{ "name": "Rinko", "age": 5 }
]),
contentType: 'application/json',
dataType: 'text',
success: function (data) {
alert(data);
},
error: function () {
alert("Server Error. Pleasy try again later.");
},
complete: function () {
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p><input id="send" value="送信" type="button" /></p>
</div>
</form>
</body>
</html>
Imports System.Web
Imports System.Web.Services
Imports Newtonsoft.Json
Public Class Ajax11
Implements System.Web.IHttpHandler
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "application/json"
Dim sr As New System.IO.StreamReader(context.Request.InputStream,
System.Text.Encoding.GetEncoding("shift_jis"))
Dim jsonStr As String = sr.ReadToEnd
Dim jsonObj As Object = JsonConvert.DeserializeObject(Of List(Of Person))(jsonStr)
For Each item In jsonObj
Debug.Print(item.name)
Debug.Print(item.age)
Next
Debug.Print(jsonStr)
context.Response.Write("成功です!")
context.Response.End()
End Sub
Public Class Person
Private _name As String
Private _age As Integer
Public Property name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property age() As Integer
Get
Return _age
End Get
Set(ByVal value As Integer)
_age = value
End Set
End Property
End Class
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class