Json.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Json.aspx.cs" Inherits="Json" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script type="text/javascript">
function Json() {
var number1 = 11;
$.ajax({
url: "WebService.asmx/AddTwoNumbers",
data:"{'x': '"+ number1+"'}",
dataType:"json",
type:"post",
contentType:"application/json; charset=utf-8",
success:function (data) {
alert(data.d);
},
error: function (data) {
alert("Error");
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Test" onclick="Json()" />
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script type="text/javascript">
function Json() {
var number1 = 11;
$.ajax({
url: "WebService.asmx/AddTwoNumbers",
data:"{'x': '"+ number1+"'}",
dataType:"json",
type:"post",
contentType:"application/json; charset=utf-8",
success:function (data) {
alert(data.d);
},
error: function (data) {
alert("Error");
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Test" onclick="Json()" />
</div>
</form>
</body>
</html>
webservice.asmx code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using
//ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string AddTwoNumbers(int x)
{
int y = 11;
int sum = x + y;
return sum.ToString();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using
//ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string AddTwoNumbers(int x)
{
int y = 11;
int sum = x + y;
return sum.ToString();
}
}
No comments:
Post a Comment