2

我正在从 jquery/ajax 调用 webmethod。有时我的 webmethod 会被调用,有时不会。我每次都传递相同的参数(数字 1 和一小段文本)。我还创建了处理程序来捕获错误并在 ajax 调用完成时显示代码。即使它没有调用我的网络方法,状态也是“成功”。有任何想法吗?

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);
}
4

2 回答 2

1

如果您的参数与之前的调用相同,则不会调用代码

    //i.e.  
    AddThread(42, "Hello World");

    //then later you also call
    AddThread(42, "Hello World");

    //the web method wont invoke any code it will just return the cached result.`

要停止此行为,您可以将 CacheDuration 设置为 0,以便它不再保存结果

[WebMethod(CacheDuration=0)]
public static void AddThread(int forumId, string comment)
{
    DataAccess.AddNewThread(forumId, comment);

}
于 2011-03-09T23:42:19.333 回答
1

我的猜测是它在 IE 中不起作用。如果是这种情况,请参阅此答案

于 2011-03-09T23:31:29.667 回答