[英]How to call a WCF service from client script using ajax?
My service contract, named IService1, is as follows 我的服务合同名为IService1,如下
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WcfServiceDemo
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
string GetData(int Value);
}
}
Here's my service implementation, 这是我的服务实现,
using System.ServiceModel.Activation;
namespace WcfServiceDemo
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(int Value)
{
return string.Format("You entered: {0}", Value);
}
}
}
My web.config, 我的web.config,
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name ="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndPointBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name ="WcfDemoService" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding" contract="IService1" behaviorConfiguration="EndPointBehavior" />
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Script for consuming WCF service, 使用WCF服务的脚本,
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Client.aspx.cs" Inherits="WcfServiceDemo.Client" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter a number:<input type="text" id="txtNum"/>
<input type="button" onclick="AlertEnteredNum();"/>
</div>
</form>
<script type="text/javascript" src="Script/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function AlertEnteredNum() {
var Value = $('#txtNum').val();
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: 'Service1.svc/GetData',
data: '{"Value": "' + Value + '"}',
dataType: "json",
processData: false,
success: function (data) {
alert("Success: " + data.d);
},
error: function (result) {
alert("error: " + result);
}
});
}
</script>
</body>
</html>
Always error callback gets executed. 总是执行错误回调。 Can someone tell me what I am missing here?
有人可以告诉我我在这里想念的吗?
I think I need to host on IIS, as I am setting compatibility mode in my config. 我想我需要在IIS上托管,因为我正在配置中设置兼容模式。 But, When I host this service on IIS, I am getting parser error as "Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive."
但是,当我在IIS上托管此服务时,解析器错误为“无法识别的属性'targetFramework'。请注意,属性名称区分大小写。”
And , if I remove this"targetFramework = 4" attribute, I am getting an exception as "Could not load file or assembly 'WcfServiceDemo' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded." 并且,如果我删除此“ targetFramework = 4”属性,则会收到异常,因为“无法加载文件或程序集'WcfServiceDemo'或其依赖项之一。此程序集由比当前加载的运行时新的运行时构建,无法被加载。”
For TargetFramework issue. 对于TargetFramework问题。 You shall check that your application pool is .net4.0 and not .net2.0
您应检查您的应用程序池是.net4.0而不是.net2.0
The javascript seems to be correct JavaScript似乎是正确的
Finally got my mistake & it's solution. 终于明白了我的错误,这是解决方案。
Just needed to change the service element in config as: 只需将config中的service元素更改为:
<services>
<service name ="WcfServiceDemo.Service1" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding" contract="WcfServiceDemo.IService1" behaviorConfiguration="EndPointBehavior" />
</service>
</services>
where name ="WcfServiceDemo.Service1" is in format "namespace.ServiceImplementationName" & contract="WcfServiceDemo.IService1" is in format "namespace.ServiceContractName" 其中名称=“ WcfServiceDemo.Service1”的格式为“ namespace.ServiceImplementationName”,而contract =“ WcfServiceDemo.IService1”的格式为“ namespace.ServiceContractName”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.