从调用jQuery的web服务的.asmx: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- code:
My JS-code:
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 = [];
}
我的Web服务:
My web service:
[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,即使没有数据变量,但它仍然给出了同样的错误。
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服务方法,使GETS:
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 GETS:
and allow GETS via web.config:
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
这篇关于从调用jQuery的web服务的.asmx:GET是不允许的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!