ajax 有时不触发 webmethod
我正在从 jquery/ajax 调用 webmethod。有时我的网络方法会被调用,有时则不会。我每次都传递相同的参数(数字 1 和一小段文本)。我还创建了处理程序来捕获错误并在 ajax 调用完成时显示代码。即使它没有调用我的 webmethod,状态也是“成功”。有什么想法吗?
jquery:
var txt = $(ta).val();
$.ajax({
type: 'POST',
url: 'Default.aspx/AddThread',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ forumId: id, comment: txt }),
dataType: 'json',
error: function(jqXHR, textStatus, errorThrown) {
alert("status: " + textStatus);
alert("errorThrown: " + errorThrown);
},
complete: function (jqXHR, textStatus) {
alert("status: " + textStatus);
}
});
C#:
[WebMethod]
public static void AddThread(int forumId, string comment)
{
DataAccess.AddNewThread(forumId, comment);
}
I'm calling a webmethod from jquery/ajax. Sometimes my webmethod gets called, other times it doesn't. I'm passing the same arguments every time (the digit 1 and a short string of text). I've also created handlers to catch error and show codes when the ajax call is complete. Even when it doesn't call my webmethod, the status is "success." Any ideas?
The jquery:
var txt = $(ta).val();
$.ajax({
type: 'POST',
url: 'Default.aspx/AddThread',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ forumId: id, comment: txt }),
dataType: 'json',
error: function(jqXHR, textStatus, errorThrown) {
alert("status: " + textStatus);
alert("errorThrown: " + errorThrown);
},
complete: function (jqXHR, textStatus) {
alert("status: " + textStatus);
}
});
The C#:
[WebMethod]
public static void AddThread(int forumId, string comment)
{
DataAccess.AddNewThread(forumId, comment);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是它在 IE 中不起作用。如果是这种情况,请参阅此答案< /a>.
my guess is that it's just not working in IE. If this is the case, see this answer.
如果您的参数与之前的调用相同,则不会调用代码
来停止此行为,您可以将 CacheDuration 设置为 0,以便不再保存结果
If your parameters are the same as a previous call it wont invoke the code
to stop this behavior you can set CacheDuration to 0 so that it no longer saves the result