gpt4 book ai didi

asp.net - 使用 jQuery 通过 GET 方法调用 ASP.NET Web 服务函数

转载 作者:行者123 更新时间:2023-12-03 22:49:08 26 4
gpt4 key购买 nike

我尝试使用 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>

因此我收到错误(调用错误处理程序)。有人知道对此可以做什么吗?

最佳答案

Enable ASP.NET ASMX web service for HTTP POST / GET requests

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string Test2()
{
[...]
}

关于asp.net - 使用 jQuery 通过 GET 方法调用 ASP.NET Web 服务函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2716363/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com