Ajax 호출 예제

Ajax 호출
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Ajax.aspx.cs" Inherits="Home.Web.Ajax" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="Js/jquery-1.11.3.js"></script>
<script src="Js/json3-3.3.2.js"></script>
<script>
$(function () {
$('#send').click(function () {
var obj = new Object();
obj.Name = $('#name').val();
obj.Name2 = $('#name').val() + '2';

var json_data = JSON.stringify({ data: obj });
alert(json_data);

$.ajax({
type: "POST",
url: "Ajax.aspx/Hello",
data: json_data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$('#result').text(data.d);
},
error: function (err) {
var errData = JSON.parse(err.responseText);
$('#result').text(errData.Message);
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Name :
<input type="text" id="name" name="name" />
<input type="button" id="send" name="send" value="전송" />
</div>
<div>
<p>결과 : <span id="result"></span></p>
</div>
</form>
</body>
</html>



---------------------------------
cs
---------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Home.Web
{
public partial class Ajax : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("sdfsdf");
}
//[WebMethod]
//public static string Hello(HelloData data)
//{
// return string.Format("Hello, {0}!", data.Name);
//}
}
//public class HelloData
// {
// public string Name { get; set; }
// public string Name2 { get; set; }
// }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Previous
Next Post »