Thursday 9 February 2012

using json and webservice in asp.net

Webservice Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

///
/// Summary description for WebService
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//[System.ComponentModel.ToolboxItem(false)]
// 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 HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string GetNamesByID(string word)
{
return word;
}


}
Design Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>













Enter Ur Name
 
Ur Name Is




Include a link in header disign code:
javascript
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"
json code:
start jscript
$(function () {
$("#btnShow").click(function () {
$.ajax(
{
type: "POST",
url: "WebService.asmx/GetNamesByID",
data: "{'word':'" + $("#txtID").val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
$("#txtName").val(msg.d);
},
error: function () {
alert('error');
}
});
});
});

end javascript

No comments:

Post a Comment