admin管理员组文章数量:1220946
I have a WCF project in VSStudio2012 and I want to call a method from JavaScript function.
JavaScript file :
var url = 'http://localhost:52768/Service1.svc/'
function test() {
var response;
$.ajax({
type: 'Post',
url: url + 'GetTEST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
alert(msg);
},
error: function (e) {
alert("Error : " + e.statusText);
}
});
}
HTML file :
<!DOCTYPE html>
<html xmlns="">
<head>
<script Language="JavaScript" src="Scripts/JavaScript1.js"></script>
<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-1.9.1.js"></script>
<script Language="JavaScript" src="Scripts/jquery-1.9.1.min.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>
<script Language="JavaScript" src="Scripts/jquery.validate.min.js"></script>
<title>TESTE</title>
</head>
<body>
<input id="Button1" type="button" value="button" onclick="test()"/>
</body>
</html>
In my IService1.cs
[OperationContract]
[WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json)]
string GetTEST();
Here is that alert display
And the error :
localhost:52768/Service1.svc display
I have a WCF project in VSStudio2012 and I want to call a method from JavaScript function.
JavaScript file :
var url = 'http://localhost:52768/Service1.svc/'
function test() {
var response;
$.ajax({
type: 'Post',
url: url + 'GetTEST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
alert(msg);
},
error: function (e) {
alert("Error : " + e.statusText);
}
});
}
HTML file :
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script Language="JavaScript" src="Scripts/JavaScript1.js"></script>
<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-1.9.1.js"></script>
<script Language="JavaScript" src="Scripts/jquery-1.9.1.min.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>
<script Language="JavaScript" src="Scripts/jquery.validate.min.js"></script>
<title>TESTE</title>
</head>
<body>
<input id="Button1" type="button" value="button" onclick="test()"/>
</body>
</html>
In my IService1.cs
[OperationContract]
[WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json)]
string GetTEST();
Here is that alert display
And the error :
localhost:52768/Service1.svc display
Share Improve this question asked Feb 23, 2014 at 16:18 JayceJayce 5952 gold badges10 silver badges25 bronze badges 4 |1 Answer
Reset to default 13Based on this article, the issue is that AJAX has cross-site limitation which prevents you to call remote service. For such cases, a simple workaround is define a server-side page_method (script callback) or local wcf service within the same application which use server-side code to call the remote WCF service. And your web page use AJAX to call the local WCF service (which works like an intermediary).
Another approach is defining your remote WCF service as a standard REST service which accept http GET request. Thus, your web page can use JQuery api to access the remote WCF service operation through JQuery script. You then host your WCF service as a console application and use JQuery in another web application to call it:
[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";
}
}
You hosting console application:
// program.cs
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)
{
// Step 1 Add a service endpoint.
using (WebServiceHost selfHost = new WebServiceHost(typeof(Service1)))
{
try
{
// Step 2 Start the service.
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
}
}
}
}
// WCF Configuration
<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>
Verify the service is working:
- Run the ConsoleAJAXWCF console application from inside Visual Studio 2012. When running on Windows Vista and later operating systems, the service must be run with administrator privileges. Because Visual Studio was run with Administrator privileges, GettingStartedHost is also run with Administrator privileges. You can also start a new command prompt running it with Administrator privileges and run service.exe within it.
- Open Internet Explorer and browse to the service's debug page at localhost:52768/Service1/
Code in ASPX page which calls the service:
<!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>
本文标签: cCall Wcf service with Javascript in Html pageStack Overflow
版权声明:本文标题:c# - Call Wcf service with Javascript in Html page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739276316a2156043.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
GetTEST()
implementation? – chridam Commented Feb 23, 2014 at 16:40