怎样用js调用wcf服务

JavaScript013

怎样用js调用wcf服务,第1张

需要System.Web.Extensions.dll(可能需要AJAXExtensionsToolbox.dll)

网上下载ASPAJAXExtSetup

使用的时候把上面的2个dll放到bin下,并添加引用(一般服务器没有安装ASPAJAXExtSetup)

==============================

Web服务

==============================

using System

using System.Web

using System.Collections

using System.Web.Services

using System.Web.Services.Protocols

using System.Web.Script.Services

using System.Data

using System.Text.RegularExpressions

/// <summary>

/// addComment 的摘要说明

/// </summary>

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[ScriptService]

public class manageComment : System.Web.Services.WebService

{

[WebMethod(EnableSession = true)]

public string CommentByID(string userName)

{

//支持session

}

public string DeleteHostComment(int replayID)

{

}

}

aspx页面设置web服务文件的路径

===============================

<asp:ScriptManager ID="smAddComment" runat="server">

<Services>

<asp:ServiceReference Path="~/WS/manageComment.asmx" />

</Services>

</asp:ScriptManager>

js调用

=================================

manageComment.CommentByID('名称')

manageComment.DeleteHostComment(1)

tip:调用的格式->[命名空间.]类名.方法名(参数1[,参数2……])

直接放在data里会把参数以json的形式直接放在body发出去,而的headers里面又设置成了content-type是application/x-www-form-urlencoded,所以就会被认为是formdata,但是其实整个都当一个key了。

当然直接用json把数据post上去其实就没问题了,如果一定要postformdata的话,应该要自己拼出来或者用jquery的$.param之类的方法来做。

什么叫这三个都是调用Ajax...除了第三个是经常用来弄ajax,前两个跟ajax有啥关系....

不过3者有一个共同点,就是代码实际上都要到服务器上去执行,然后可以在前段用js获取结果,传输数据都是用xml。

.ashx貌似我见过的基本上都是为ajax服务。

而wcf和webservice实际上是为了分布式、跨平台的程序服务。

简单的说,微软做出wcf这样一个东西,你可以理解为

wcf的优点=remoting(局域网使用很爽)+webservice(分布式、跨平台)+安全性