小编典典

jQuery:对asp.net Web服务的Ajax调用失败,服务器返回500错误

ajax

好的,所以我创建了一个测试项目,只是为了验证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,网络服务可以正常运行。

还有什么?


阅读 223

收藏
2020-07-26

共1个答案

小编典典

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

2020-07-26