开发者

Calling WCF Service Operations in AJAX

开发者 https://www.devze.com 2023-01-29 04:58 出处:网络
I have one asp.net application, in which i am using \"Calling WCF Service Operations in AJAX\" method. I finished my work without the service hosting in IIS. I got the correct solution. But now, i hav

I have one asp.net application, in which i am using "Calling WCF Service Operations in AJAX" method. I finished my work without the service hosting in 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. But in clicking the button i got one javascript error like object expected. 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) {
    documen开发者_如何转开发t.getElementById('addAnswer').value = result;
}
function onSubtractSuccess(result) {
    document.getElementById('subtractAnswer').value = result;
}

the javascript error message is like this

Calling WCF Service Operations in AJAX

Please help me for solving this issue.


Issue is with java-script such as Service1.Add... The name of generated js proxy would not be Service1 and hence the issue. 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. If you haven't specified one the default is "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

0

精彩评论

暂无评论...
验证码 换一张
取 消