【问题标题】:Connect ajax to web service in asp.net将 ajax 连接到 asp.net 中的 Web 服务
【发布时间】:2014-11-20 09:53:54
【问题描述】:

您好,我正在使用带有 web 服务的 ajax 将表单信息添加到数据库中:

网络服务代码是:

[WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

而ajax是:

$.ajax({
                        type: "GET",
                        url: "WebService.asmx/HelloWorld",
                        data: {},
                        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                        dataType: "text",
                        success: function (data) {
                            alert(date);
                        },
                        failure: function (msg) { alert("Sorry!!! "); }

                    });

但是当我运行代码时,我得到了这个错误:

GET http://localhost:53145/Group/WebService.asmx/HelloWorld 500 (Internal Server Error) 

有人可以帮我吗? ajax代码和webservice在同一个文件夹,名字对吗?

【问题讨论】:

  • 扩展名 .asmx 不受支持,或者您可以将其路由,因此将其删除
  • 它对我不起作用

标签: jquery asp.net ajax web-services


【解决方案1】:

您应该将此添加到您的 web.config 文件中:

<system.web>        
    <webServices>
        <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
        </protocols>
    </webServices>
</system.web>

或者把方法从GET改成POST:

.ajax({
    type: "POST",
    url: "WebService.asmx/HelloWorld",
    .....

【讨论】:

    【解决方案2】:

    错误清楚地表明 ajax 请求无法找出 web 服务的路径。我建议使用浏览器调试工具来解决路径问题。只需按 F12 并打开调试模式。有关网络服务的详细使用情况,请使用以下链接。

    http://forums.asp.net/t/1934215.aspx?Using+jQuery+ajax+to+call+asmx+webservice+methods

    【讨论】:

    • 我看到了这篇文章,但它没有给我解决方案
    • 这篇文章仅供参考 你的代码没有问题 唯一的问题是你的webservice路径??您是否尝试过使用浏览器调试工具来解决问题..
    • 在浏览器调试工具中,进入网络选项卡下,如果您的路径良好,则查看您获取并发布请求,然后您将获得 200 ok,否则会出现 500 未找到问题。
    猜你喜欢
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    相关资源
    最近更新 更多