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

jQuery:Ajax调用asp.net webservice失败,服务器返回500错误,asp.net,ajax,jquery,Asp.net,Ajax,Jquery,好的,所以我创建了一个测试项目,只是为了验证jQueryAjax是否可以与asp.net服务一起工作,并且它没有任何问题。我使用了在VS studio中创建的默认HelloWorld服务。我通过jQuery调用该服务,如下所示: 在Default.aspx中: <script language="javascript" type="text/javascript"> $(document).ready(function() { //test web serv

好的,所以我创建了一个测试项目,只是为了验证jQueryAjax是否可以与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>

$(文档).ready(函数(){
//测试web服务
$.ajax({
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
url:“TestService.asmx/HelloWorld”,
数据:“{}”,
数据类型:“json”,
成功:函数(msg){alert(msg);},
错误:函数(XMLHttpRequest、textStatus、errorshown){
调试器;
}
});
});
在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";
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Services;
命名空间WebServiceTestWitJQuery
{
/// 
///TestService的摘要描述
/// 
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类TestService:System.Web.Services.WebService
{
[网络方法]
公共字符串HelloWorld()
{
返回“你好世界”;
}
}
}
然后我继续并复制了所有的东西,就像我的项目中一样,但它不起作用。我得到一个500服务器错误

我核实了以下情况:

  • web.com配置相同
  • 页数相同
  • 服务等级相同
  • jqueryajax调用相同
  • 我可以导航到Web服务,Web服务工作正常

  • 还有什么?

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

    我有一些猜测,因为我对jQuery了解一点,但对asp.net一无所知

    在jQuery调用中,您表示需要json响应。我认为“Hello World”将作为原始字符串返回,而不是json格式。删除'dataType:'json'、'param和jQuery应尽可能确定接收到的数据类型


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

    问题在于您的脚本URL中,因此您需要编写完整的URL。这是你的解决方案。 如:
    URL:localhost:1111/TestService.asmx/HelloWorld
    我解决了这个问题。我知道回答这个问题太晚了,但可能有人也有同样的问题

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

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

    我收到这个错误,它是由我的Web服务无法访问会话状态引起的。这是通过改变

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


    有关更多信息,请参见此链接:

    我发布此文章,因为两个顶级解决方案都不适合我(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>
    
    
    --仅用于直接测试:删除此行后
    
    此外,如果服务位于子文件夹中,则必须指定完整路径。
    <system.web>
        <webServices>
         <protocols> 
        <add name="HttpGet" /> -- just for direct testing : delete this line after 
        <add name="HttpPost" />
         </protocols>
         </webServices>
    </system.web>