无法调用Web服务在ASP.NET中的Javascript [英] Fail to Call WebServices in ASP.NET in Javascript

查看:117
本文介绍了无法调用Web服务在ASP.NET中的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不叫web服务。我有什么错过? @@

I fail to call a webservice. What have I miss? @@

我想在这里遵循此说明:<一href=\"http://stackoverflow.com/questions/4106026/how-to-call-an-asp-net-web-service-in-javascript\">How调用一个ASP.Net Web服务的JavaScript

I am trying to follow this instructions here: How to call an ASP.Net Web Service in javascript

和下面是我的codeS,但无法工作。

And below are my codes, but fails to work.

这是我的web codeS:

This is my webservice codes:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

namespace Library
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService()]
    public class ReleaseSessionObject : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWord()
        {
            return "Hellow Word!";
        }
    }
}

这就是我的ASP.NET页面codeS:

and this is my ASP.NET page codes:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm6.aspx.cs" Inherits="Library.WebForm6" %>

<!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>

</head>
<body>
    <form id="form1" runat="server">
    <div>

    <script src="ReleaseSessionObject.asmx" type="text/javascript"></script>
    <script type="text/javascript">
        function HelloWorld() {
            try {
                var a = Library.ReleaseSessionObject.HelloWorld();
                alert(a);
            }
            catch (err) {
                alert(err.Message); 
            }
        };
    </script>

    <asp:ScriptManager ID="ScriptManager1" runat="server">
      <Services>
        <asp:ServiceReference Path="~/ReleaseSessionObject.asmx" />
      </Services>
    </asp:ScriptManager>

    <input type="button" onclick="HelloWorld();" value="Click This" />

    </div>
    </form>
</body>
</html>

既ASP.NET页面和服务文件位于同一文件夹。

both ASP.NET page and Service files are located at the same folder.

但是当我点击按钮。它说:未定义

but when I click on the button. It says: undefined

推荐答案

尝试使异步:

function HelloWorld() {
    Library.ReleaseSessionObject.HelloWorld(OnCompleted);
}

function OnCompleted(data) {
    alert(data);
}

这篇关于无法调用Web服务在ASP.NET中的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆