如何使用带有字符串以外参数的 jQuery 来使用 WebMethod?
我有这个客户端:
$(document).ready(function() {
var $checkboxes = $('[type=checkbox]');
$checkboxes.click(function() {
$checkbox = $(this);
$.ajax({
type: "POST",
url: "Page.aspx/Method",
data: "{id:'" + $checkbox.attr("id") + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert('success!');
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert('An error occurred that prevented your change being saved: ' + err.Message);
}
});
});
});
我有这个服务器端:
[WebMethod]
public static void Method(String id)
{
Guid guidID = new Guid(classid));
//...........
}
但我更喜欢将 WebMethod 签名强类型化:
[WebMethod]
public static void Method(Guid id)
{
//...........
}
你能建议什么是最好的方法吗?
I have this client-side:
$(document).ready(function() {
var $checkboxes = $('[type=checkbox]');
$checkboxes.click(function() {
$checkbox = $(this);
$.ajax({
type: "POST",
url: "Page.aspx/Method",
data: "{id:'" + $checkbox.attr("id") + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert('success!');
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert('An error occurred that prevented your change being saved: ' + err.Message);
}
});
});
});
And I have this server-side:
[WebMethod]
public static void Method(String id)
{
Guid guidID = new Guid(classid));
//...........
}
But I'd prefer to make the WebMethod signature strongly typed:
[WebMethod]
public static void Method(Guid id)
{
//...........
}
Can you advise what is the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您可以将 Guid 作为字符串传递到您的方法中,它应该将其转换。 看看这个
I think you can pass the Guid as a string into your method and it should convert it. check this out
您可以拥有强类型参数。如果它们的格式正确,它们将在调用该方法时隐式转换为正确的类型。
You can have strongly typed parameter(s). If they're in the correct format, they'll be implicitly converted to the correct type upon hitting the method.