【问题标题】:how to call web service from a web-user control in asp.net?如何从asp.net 中的Web 用户控件调用Web 服务?
【发布时间】:2013-03-20 10:55:42
【问题描述】:

我有一个网络用户控件,我想从中调用网络服务。我的主要动机是什么:

1.我正在为高级搜索创建一个网络用户控件,为此我将绑定的字段和按钮[编辑,删除]动态添加到网格视图。 2. 现在我正在使用 ajax 进行编辑和删除(有特定的理由采用这种方法,因为我已经提到我正在动态添加边界字段和按钮,因为首先我清除了网格的所有列然后添加新的。现在如果按钮的点击触发,那么它的回发页面,这使得按钮将从网格中消失,所以我想使用 Json)

这是我的网格代码::

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4"
                BorderWidth="1px" ForeColor="#333333" GridLines="None">
                <AlternatingRowStyle BackColor="White" />
                <FooterStyle BackColor="#990000" ForeColor="White" Font-Bold="True" />
                <HeaderStyle Height="30px" BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <PagerStyle ForeColor="#333333" HorizontalAlign="Center" BackColor="#E2E2E2" />
                <RowStyle CssClass="test" BackColor="#E2E2E2" Height="25px" BorderWidth="1px" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2E2E2" Font-Bold="True" ForeColor="Navy" />
                <SortedAscendingCellStyle BackColor="#FDF5AC" />
                <SortedAscendingHeaderStyle BackColor="#4D0000" />
                <SortedDescendingCellStyle BackColor="#FCF6C0" />
                <SortedDescendingHeaderStyle BackColor="#820000" />
            </asp:GridView>

这是Json调用的代码

$('.DeleteButton').live('click', function () {
        alert('hellllo');
        $.ajax({
            url: '<%=ResolveUrl("~/Control/WebService/WebService.asmx/HelloWorld") %>',
            data: "{ }",
            dataType: "json",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            success: function () {
                alert('hi;');
            },
            error: function () {
                alert('Record could not be deleted. \n Please try again later. hye');
            },
            failure: function () {
                alert('Record could not be deleted. \n Please try again later. hello');
            }
        });
    });

我总是收到以下错误:

500 个内部错误 请帮助我摆脱这个问题,我从过去 2 天开始就被困住了。

【问题讨论】:

  • 500 表示错误在你的服务中。
  • @Schaliasos 什么样的错误,我该如何解决
  • Internal Server Error,正如您在其描述中看到的那样。您需要以某种方式找出您的服务出了什么问题。您使用日志记录吗?
  • 检查您的网址内容,将其替换为另一个示例工作网址并检查是否成功!深入研究
  • @codebrain 我已经检查过它是否创建了正确的路径并在另一个页面中工作

标签: c# asp.net json web-services webusercontrol


【解决方案1】:

如果您使用chrome浏览器调试,请执行以下操作:

  1. 按 F12 打开开发人员(如果名称错误,请更正我)面板
  2. 将选项卡设置为网络选项卡
  3. 调用方法

如果发生内部服务器错误 500,那么您会在底部某处找到一条带有红色字体的记录。您可以使用其中所述的 URL 进行调试。那里的信息也为您提供响应信息。

此外,您可以尝试使用激活到 Web 服务的 Visual Studio 调试来调试该服务。然后尝试调用 web 服务,看看它是否捕获到任何异常。

【讨论】:

  • 我还有其他浏览器,我正在检查 firfox
  • 在firefox中也许你可以使用firebug,但我自己没有确定使用过,所以我不知道它是否可以像chrome一样工作
【解决方案2】:

500 内部错误 - 我可以猜测是:
不要使用

var html = '<%=ResolveUrl("~/Control/WebService/WebService.asmx/HelloWorld") %>';

改为使用:

var html= '<%=ResolveUrl("full path /Control/WebService/WebService.asmx/HelloWorld") %>';

var html = '<%=ResolveUrl("../../Control/WebService/WebService.asmx/HelloWorld") %>';

【讨论】:

    【解决方案3】:

    我已经为我解决了如下问题:

    jQuery 代码是:

    $(document).ready(function () {
    $('.DeleteButton').live('click', function (e) {
        var td = $(this).parent('td').parent('tr');
        if (confirm('Do you want to delete this record?')) {
            var Index = $(this).attr('id');
            var pos = Index.indexOf("_");
            var position = Index.indexOf("_");
            while (pos > -1) {
                pos = Index.indexOf("_", pos + 1);
                if (pos > -1) {
                    position = pos;
                }
            }
            Index = Index.substr(position + 1);
    
            var Id = $(this).attr('alt');
            var TableName = $('[id$=hdfTableName]').val();
            var PrimaryKey = $('[id$=hdfPrimaryKey]').val();
    
            $.ajax({
                type: "POST",
                url: "Search.aspx/DeleteRecord",
                data: "{ 'Id': '" + Id + "','TableName':'" + TableName + "','PrimaryKey':'" + PrimaryKey + "' }",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    if (msg.d == '1') {
                        td.hide();
                        $('.lblMessage').addClass("SuccessMessage");
                        $('.lblMessage').text('Record is deleted sucessfuly.');
                        $('.lblMessage').delay('5000').fadeOut('10000');
                    } else {
                        $('.lblMessage').addClass("ErrorMessage");
                        $('.lblMessage').text('There is some error, while deleting the record.');
                        $('.lblMessage').delay('5000').fadeOut('10000');
                    }
                }
            });
        }
    });
    $('.EditButton').live('click', function (e) {
        var Id = $(this).attr('alt');
        window.location.href = 'Default.aspx?id=' + Id + '';
    });
    

    });

    我已经从以下页面调用了 Web 方法:

    [WebMethod]
    public static string DeleteRecord(string Id, string TableName, string PrimaryKey)
    {
        String result = "";
        try
        {
            Id = "'" + Id + "'";
            clsSearch objSearch = new clsSearch();
            result = objSearch.DeleteRecord(TableName, PrimaryKey, Id);
            result = "1";
        }
        catch (Exception ex)
        {
            result = ex.Message;
        }
        return result;
    }
    

    顺便谢谢你的回复.....

    【讨论】:

      猜你喜欢
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 2011-03-31
      • 1970-01-01
      相关资源
      最近更新 更多