62,046
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="jquery-1.3.1.min.js" type="text/javascript"></script>
</head>
<body>
<script>
function Speak() {
var pople = {a:1,b:2};
$.ajax({
type: 'post',
url: '/JS_WCF/MyServices.svc/Add',
contentType: 'text/json',
data: JSON2.stringify(pople),
success: function(msg) {
alert(msg);
}
});
}
</script>
</body>
</html>
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
/// <summary>
///MyServices 的摘要说明
/// </summary>
///
[ServiceContract(Namespace="")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyServices
{
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Add", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public int Add(int a,int b)
{
//
//TODO: 在此处添加构造函数逻辑
//
return a + b;
}
}
<%@ ServiceHost Language="C#" Debug="true" Service="MyServices" CodeBehind="~/App_Code/MyServices.cs" %>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="me">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"></serviceHostingEnvironment>
<services>
<service name="MyServices">
<endpoint address="" binding="webHttpBinding" contract="MyServices" behaviorConfiguration="me">
</endpoint>
</service>
</services>
</system.serviceModel>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
namespace WcfService1
{
// 注意: 如果更改此处的接口名称 "IService1",也必须更新 Web.config 中对 "IService1" 的引用。
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// 任务: 在此处添加服务操作
}
// 使用下面示例中说明的数据约定将复合类型添加到服务操作。
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
namespace WcfService1
{
// 注意: 如果更改此处的类名“Service1”,也必须更新 Web.config 和关联的 .svc 文件中对“Service1”的引用。
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
<%@ ServiceHost Debug="true" Language="C#" Service="WcfService1.Service1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="jquery-1.2.6.js" type="text/javascript"></script>
<script src="JSON2.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<script>
function Speak() {
$.ajax({
type: 'post',
url: '/JS_WCF/WcfService1.svc/GetData',
contentType: 'text/json',
data: 1,
success: function(msg) {
alert(msg);
}
});
}
Speak();
</script>
</div>
</form>
</body>
</html>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="bbb">
<webHttp/>
</behavior>
<behavior name="other">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"></serviceHostingEnvironment>
<services>
<service name="MyServices">
<endpoint address="" binding="webHttpBinding" contract="MyServices" behaviorConfiguration="bbb">
</endpoint>
</service>
<service name="WcfService1">
<endpoint binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="other"></endpoint>
</service>
</services>
</system.serviceModel>
</configuration>