, , AJAX - , . page_method (script callback) wcf , WCF. - AJAX WCF ( ).
WCF REST, HTTP- GET. , - JQuery api WCF JQuery script. WCF JQuery - :
[ServiceContract(Namespace="ConsoleAJAXWCF")]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
string GetTEST();
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetTEST()
{
return "OKKKKKKKK";
}
}
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using ConsoleAJAXWCF;
namespace ConsoleAJAXWCF
{
class Program
{
static void Main(string[] args)
{
using (WebServiceHost selfHost = new WebServiceHost(typeof(Service1)))
{
try
{
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
}
}
}
}
<endpointBehaviors>
<behavior name="AJAXEndpoint" >
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="ConsoleAJAXWCF.Service1">
<endpoint
behaviorConfiguration="AJAXEndpoint"
address="" binding="webHttpBinding" contract="ConsoleAJAXWCF.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:52768/Service1/"/>
</baseAddresses>
</host>
</service>
</services>
, :
- ConsoleAJAXWCF Visual Studio 2012. Windows Vista . Visual Studio , GetStartedHost . service.exe .
- Internet Explorer localhost: 52768/Service1/
ASPX, :
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script Language="JavaScript" src="Scripts/jquery-1.10.2.min.js"></script>
<script Language="JavaScript" src="Scripts/jquery-1.9.1.intellisense.js"></script>
<script Language="JavaScript" src="Scripts/jquery.unobtrusive-ajax.js"></script>
<script Language="JavaScript" src="Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script Language="JavaScript" src="Scripts/jquery.validate.js"></script>
<title>TESTE</title>
</head>
<body>
<input id="Button1" type="button" value="button" onclick="CallRESTWCFService()"/>
</body>
<script type="text/javascript">
function CallRESTWCFService() {
$.getJSON("http://localhost:52768/Service1/GetTEST", {},
function (data) {
alert(data);
});
}
</script>
</html>