从 jQuery 调用 .asmx webservice:不允许 GET?

Lar*_*ard 1 asp.net jquery web-services asmx .net-4.5

我有一个简单的页面。加载时,它调用 Web 服务,然后出现以下错误:

an attempt was made to call the method using a GET request, which is not allowed
Run Code Online (Sandbox Code Playgroud)

我的JS代码:

    function getTutors() {
        var url = '<%= ResolveUrl("~/services/tutorservice.asmx/gettutors") %>';
        $.ajax({
            type: "GET",
            data: "{'data':'" + 'test-data' + "'}",
            url: url,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (d) {
                alert('succes');
                return d;
            },
            error: function () {
                alert('fejl');
            }
        });
    }

    $(document).ready(function () {
        var tutors = getTutors();
        var locations = [];
    }
Run Code Online (Sandbox Code Playgroud)

我的网络服务:

    [ScriptService]
public class tutorservice : System.Web.Services.WebService {

    public tutorservice () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public List<Tutor> gettutors(string data)
    {
        var tutorManager = new TutorManager();
        return tutorManager.GetTutorApplicants();
    }

}
Run Code Online (Sandbox Code Playgroud)

我试图删除 contentTypes,即使没有数据变量,它仍然会给出相同的错误。

我最好的猜测是应该删除一些 contentType / dataType,但我也尝试过。

关于我为什么会收到此错误的任何想法?

小智 5

我能想到两个选择:

1) 在 AJAX 调用中使用 POST 而不是 GET:

type: "POST",
Run Code Online (Sandbox Code Playgroud)

或 2) 如果您必须使用 GET,请配置您的 Web 服务方法以允许使用 GET:

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public List<Tutor> gettutors(string data)
{
    var tutorManager = new TutorManager();
    return tutorManager.GetTutorApplicants();
}
Run Code Online (Sandbox Code Playgroud)

并通过 web.config 允许获取:

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>
Run Code Online (Sandbox Code Playgroud)