简体   繁体   English

在AJAX中调用WCF服务操作

[英]Calling WCF Service Operations in AJAX

I have one asp.net application, in which i am using "Calling WCF Service Operations in AJAX" method. 我有一个asp.net应用程序,在其中我正在使用“在AJAX中调用WCF服务操作”方法。 I finished my work without the service hosting in IIS. 我在没有IIS托管服务的情况下完成了工作。 I got the correct solution. 我得到了正确的解决方案。 But now, i have another requirement in which i am using the WCF service which is hosted on local host iis. 但是现在,我还有另一个要求,我要使用本地主机iis上托管的WCF服务。 But in clicking the button i got one javascript error like object expected. 但是在单击按钮时,我遇到了一个JavaScript错误,如预期的对象。 I don't know why this happened? 我不知道为什么会这样? My code is shown below. 我的代码如下所示。

<fieldset style="width: 804px" align="center">
    <legend>Consuming WCF Service using Client-Side AJAX</legend>
    <div align="left" style="text-align: center">
        <form id="form1" runat="server">
        <asp:ScriptManager ID="SM1" runat="server">
            <Services>
                <asp:ServiceReference Path="http://localhost/WCFService/Service1.svc" />
                <%--~/Service1.svc--%>
            </Services>
        </asp:ScriptManager>
        <input id="addNum1" type="text" size="3" />
        +
        <input id="addNum2" type="text" size="3" />
        =
        <input id="addAnswer" type="text" size="3" /><br />
        <input id="btnAddition" type="button" value="Do Addition" onclick="DoAddition()" />
        <br />
        <br />
        <input id="subtractNum1" type="text" size="3" />
        -
        <input id="subtractNum2" type="text" size="3" />
        =
        <input id="subtractAnswer" type="text" size="3" /><br />
        <input id="btnSubtraction" type="button" value="Do Subtraction" onclick="DoSubtraction()" />
        <br />
        <br />
        </form>
    </div>
</fieldset>

<script language="javascript" type="text/javascript">

function DoAddition() {
    Service1.Add(document.getElementById('addNum1').value, document.getElementById('addNum2').value, onAddSuccess);
}
function DoSubtraction() {
    Service1.Subtract(document.getElementById('subtractNum1').value, document.getElementById('subtractNum2').value, onSubtractSuccess);
}

function onAddSuccess(result) {
    document.getElementById('addAnswer').value = result;
}
function onSubtractSuccess(result) {
    document.getElementById('subtractAnswer').value = result;
}

the javascript error message is like this javascript错误消息是这样的

图片

Please help me for solving this issue. 请帮助我解决此问题。

Issue is with java-script such as Service1.Add.. . Java脚本(例如Service1.Add.. The name of generated js proxy would not be Service1 and hence the issue. 生成的js代理的名称将不是Service1,因此是问题所在。 The name of proxy object would be of form [Namespace].[Contract Name] where namespace of the service, as declared by the Namespace parameter of the ServiceContract attribute. 代理对象的名称将采用[Namespace].[Contract Name] ,其中服务的名称空间由ServiceContract属性的Namespace参数声明。 If you haven't specified one the default is " tempuri.org ". 如果您未指定,则默认为“ tempuri.org ”。 For example, if your service is defined as 例如,如果您的服务定义为

[ServiceContract(Namespace="Samples.Services")]
public class Service1
{
  [OperationContract]
  public void Add(...

Then in js, you need to use Sample.Services.Service1.Add 然后在js中,您需要使用Sample.Services.Service1.Add

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM