我尝试使用 jQuery 通过 GET 方法调用 Web 服务函数,但遇到问题。这是一个网络服务代码:
[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;
}
}
这就是我调用该函数的方式:
$.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 标签覆盖,如下所示:
<string>
{'result':'success', 'datetime':'4/26/2010 12:11:18 PM'
</string>
因此我收到一个错误(调用错误处理程序)。有人知道对此可以做什么吗?