// Javascript
var Type;
var Url;
var Data;
var ContentType;
var DataType;
var ProcessData;
function CallService() {
$.ajax({
type: Type, //GET or POST or PUT or DELETE verb
url: Url, // Location of the service
data: Data, //Data sent to server
contentType: ContentType, // content type sent to server
dataType: DataType, //Expected data format from server
processdata: ProcessData, //True or False
success: function (msg) {//On Successfull service call
ServiceSucceeded(msg);
},
error: ServiceFailed// When Service call fails
});
}
function ServiceSucceeded(result) {
alert("muthuvel");
if (DataType == "json") {
resultObject = result.GetUserResult;
for (i = 0; i < resultObject.length; i++) {
alert(resultObject[i]);
}
}
}
function ServiceFailed(xhr) {
alert("Failed");
alert(xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}
function getName() {
var uesrid = "1";
Type = "POST";
Url = "Services/Service.svc/GetUser";
Data = '{"Id": "' + uesrid + '"}';
ContentType = "application/json; charset=utf-8";
DataType = "json"; ProcessData = true;
CallService();
}
// WCF Service
// Service.svc
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Activation;
namespace AssessmentmanagerHTML5
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public string[] GetUser(string Id)
{ return new User().GetUser(Convert.ToInt32(Id)); }
}
public class User
{
Dictionary users = null;
public User()
{
users = new Dictionary();
users.Add(1, "pranay");
users.Add(2, "Krunal");
users.Add(3, "Aditya");
users.Add(4, "Samir");
}
public string[] GetUser(int Id)
{
var user = from u in users
where u.Key == Id
select u.Value;
return user.ToArray();
}
}
}
// IService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
namespace AssessmentmanagerHTML5
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService" in both code and config file together.
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
string[] GetUser(string Id);
}
}
No comments:
Post a Comment