从 jQuery 调用 .asmx webservice:不允许 GET? [英] Calling .asmx webservice from jQuery: GET is not allowed?
问题描述
我有一个简单的页面.加载时,它调用 Web 服务,然后出现以下错误:
I have a simple page. On load, it calls a web service, and then I get the following error:
an attempt was made to call the method using a GET request, which is not allowed
我的 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 = [];
}
我的网络服务:
[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();
}
}
我尝试删除 contentTypes,即使没有 data 变量,它仍然给出相同的错误.
I have tried to remove the contentTypes, and even without the data variable, it still gives the same error.
我最好的猜测是应该删除某些 contentType/dataType,但我也尝试过.
My best guess is some contentType / dataType should be removed, but I have tried that as well.
关于为什么我会收到此错误的任何想法?
Any ideas about why I get this error?
推荐答案
我能想到两个选择:
1) 在 AJAX 调用中使用 POST 而不是 GET:
1) Use a POST instead of a GET in your AJAX call:
type: "POST",
或 2) 如果您必须使用 GET,请配置您的 Web 服务方法以允许使用 GET:
or 2) If you must use a GET, configure your web service method to allow GETS:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public List<Tutor> gettutors(string data)
{
var tutorManager = new TutorManager();
return tutorManager.GetTutorApplicants();
}
并通过 web.config 允许获取:
and allow GETS via web.config:
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
这篇关于从 jQuery 调用 .asmx webservice:不允许 GET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!