【问题标题】:jQuery: Ajax call to asp.net webservice fails, server return 500 errorjQuery:对 asp.net Web 服务的 Ajax 调用失败,服务器返回 500 错误
【发布时间】:2010-01-08 13:53:46
【问题描述】:

好的,所以我创建了一个测试项目,只是为了验证 jQuery AJAX 是否可以与 asp.net 服务一起使用,并且没有问题。我使用了在 VS Studio 中创建的默认 HelloWorld 服务。我像这样通过 jQuery 调用服务:

在 Default.aspx 中:

<script language="javascript" type="text/javascript">
    $(document).ready(function() {

        //test web service
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "TestService.asmx/HelloWorld",
            data: "{}",
            dataType: "json",
            success: function(msg) { alert(msg);},
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                debugger;
            }
        });
    });
</script>

在 TestService.asmx 中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebServiceTestWitJQuery
{
    /// <summary>
    /// Summary description for TestService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class TestService : System.Web.Services.WebService
    {

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

然后我继续并完全按照我的项目中的内容复制了所有内容,但它不起作用。我收到 500 服务器错误。

我验证了以下内容:

  1. web.configs 相同
  2. 页面相同
  3. 服务类相同
  4. jquery ajax 调用相同
  5. 我可以导航到http://localhost:3272/TestService.asmx?op=HelloWorld 并且网络服务工作正常。

还有什么?

【问题讨论】:

    标签: asp.net ajax jquery


    【解决方案1】:

    想通了。如果不确定发生了什么,请使用 Fiddler。它清楚地表明服务器无法创建服务类的实例,因为它位于错误的命名空间中。我禁用了 R#,但没有注意到我没有更改服务的命名空间。问题解决了。

    【讨论】:

    • 另外,如果您的服务在子文件夹中,您必须指定完整路径。
    【解决方案2】:

    我有几个猜测,因为我对 jQuery 有所了解,但对 asp.net 却一无所知。

    在 jQuery 调用中,您表明您想要一个 json 响应。我认为“Hello World”将作为原始字符串返回,而不是格式化为 json。去掉 'dataType: "json",' 参数,jQuery 应该尽量确定接收到的数据类型。

    我认为您编写 jQuery 代码的方式仅与 RESTful Web 服务兼容,而不与具有 WSDL 的传统 Web 服务兼容。虽然我对 asp.net 一无所知,但我在您使用 REST 的命名约定中看不到任何内容。

    【讨论】:

      【解决方案3】:

      问题在于您的脚本 URL,因此您需要编写完整的 URL。 Here 是您的解决方案。 例如: URL:localhost:1111/TestService.asmx/HelloWorld

      【讨论】:

        【解决方案4】:

        我解决了这个问题。我知道回答这个问题为时已晚,但可能有人有同样的问题。

        我从 ajax 中删除了下面的行,并禁用了 500 个内部错误。

        contentType: "application/json; charset=utf-8",
                data: "{}",
                dataType: "json",
        

        【讨论】:

          【解决方案5】:

          我收到此错误,这是由于我的 WebService 无法访问会话状态引起的。这是通过改变来解决的

          [WebMethod]
          public MyData GetMyData() {...}
          

          [WebMethod(EnableSession = true)]
          public MyData GetMyData() {...}
          

          查看此链接了解更多信息:How to use Session state in ASP.NET WebService

          【讨论】:

            【解决方案6】:

            我发布了这个,因为这两个顶级解决方案都不适合我(VS2015、.Net4.0、WebForms 中的 ajax 调用与问题中的完全一样)。 它只有在我像这样在配置文件中明确授权 HttpPost 后才起作用:

            <system.web>
                <webServices>
                 <protocols> 
                <add name="HttpGet" /> -- just for direct testing : delete this line after 
                <add name="HttpPost" />
                 </protocols>
                 </webServices>
            </system.web>
            

            【讨论】:

              猜你喜欢
              • 2010-10-29
              • 2012-08-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-01-19
              • 2013-09-13
              • 1970-01-01
              • 2017-10-28
              相关资源
              最近更新 更多