通过GET方法与jQuery调用ASP.NET Web服务功能 [英] Calling ASP.NET web service function via GET method with jQuery
问题描述
我试图调用使用jQuery通过GET方法,Web服务的功能,但有一个问题。这是一个Web服务code:
I'm trying to call web service function via GET method using jQuery, but having a problem. This is a web service code:
[WebService(Namespace = "http://something.com/samples")]
[ScriptService]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TestWebService : System.Web.Services.WebService {
[WebMethod]
public string Test2()
{
string result = null;
try
{
result = "{'result':'success', 'datetime':'" + DateTime.Now.ToString() + "'";
}
catch (Exception ex)
{
result = "Something wrong happened";
}
return result;
}
}
这是我调用该函数的方式:
That's the way I call the function:
$.ajax({ type: "GET",
url: "http://localhost/testwebsite/TestWebService.asmx/Test2",
data: "{}",
contentType: "application/json",
dataType: "json",
error: function (xhr, status, error) {
alert(xhr.responseText);
},
success: function (msg) {
alert('Call was successful!');
}
});
方法调用成功,但结果字符串被覆盖的XML标签,就像这样:
Method is called successfully, but result string gets covered by XML tags, like this:
<string>
{'result':'success', 'datetime':'4/26/2010 12:11:18 PM'
</string>
和我得到的,因为这一个错误(错误处理程序被调用)。有谁知道什么可以做什么呢?
And I get an error because of this (error handler is called). Does anybody know what can be done about this?
推荐答案
<一个href="http://stackoverflow.com/questions/618900/enable-asp-net-asmx-web-service-for-http-post-get-requests">http://stackoverflow.com/questions/618900/enable-asp-net-asmx-web-service-for-http-post-get-requests
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string Test2()
{
[...]
}
这篇关于通过GET方法与jQuery调用ASP.NET Web服务功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!