【问题标题】:jquery Ajax not responding?jquery Ajax 没有响应?
【发布时间】:2011-05-18 20:31:05
【问题描述】:

所以我在一个应该调用 asp.net webservice 的页面上有以下功能,它似乎正在这样做,但页面上什么也没发生。下面是webservice下面的函数

$("#BlogSelectList li a").click(function () {
    var str = ($(this).attr("href")).slice(1, 36)
    $.ajax({
        contentType: "application/json; charset=utf-8",
        url: '../ws/WebServices.asmx/SetActiveBlog',
        data: '{ActiveBlogID: "' + str + '"}',
        dataType: 'json',
        type: "post",
        success: function (j) {
            if (j.d == 1) {
                window.location('http://www.msn.com');
            }
            else {
                window.location('http://www.msn2.com');
            }
            alert('heyhi')
        }, error: function (j) {
            alert(':(')
        }

    });

});

这是 web 服务,我知道它正在执行,因为它正在运行一个存储过程,该过程使用“ssss”成功创建了一个日志条目,但是当单击锚点时页面实际上什么都不做,它不会重定向页面,它不会'不做任何警报,什么都不做。

[WebMethod(Description = "Sets the ActiveBlog.")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public bool SetActiveBlog(string ActiveBlogID)
{
    DbaseExecSpWithReturnValue Sproc = new DbaseExecSpWithReturnValue();
    Sproc.SetSp("sp_CheckUsernameAvailable");
    Sproc.AddParam(1);
    Sproc.AddParam("Username", SqlDbType.Char, "ssss", 20);
    int RetVal = Sproc.Execute();
    Sproc.Close();
    return true;
}

【问题讨论】:

  • 您是否使用 Firebug 或类似工具检查过 Ajax 是否成功完成?
  • 检查成功回调中的变量j 是否不为空或未定义。我想如果有 json 解析错误,它可能是可能的。它可以解释为什么您看不到任何警报,因为在您的 if 条件中检查 j.d 会使您的回调函数崩溃。

标签: jquery asp.net ajax web-services json


【解决方案1】:

window.location 不是你调用的函数,而是你设置的属性:

if (j.d == 1) {
    window.location = 'http://www.msn.com';
}

等等

【讨论】:

  • 啊,你说的太对了。我把它作为一个属性但是没有注册 web 服务,所以我把它改成了一个函数,以为我错了,然后忘了注册 web 服务,忘了改回来!!谢谢
【解决方案2】:

我相信这是因为您调用的是window.location() 而不是设置window.location.href = 'someUrl'

【讨论】:

    【解决方案3】:
    $("#BlogSelectList li a").click(function () {
        var str = $(this).attr("href").slice(1, 36);
        $.ajax({
            contentType: "application/json; charset=utf-8",
            url: '../ws/WebServices.asmx/SetActiveBlog',
            data: '{ActiveBlogID: "' + str + '"}',
            dataType: 'json',
            type: "post",
            success: function (j) {
                if (j.d == 1) {
                    window.location = 'http://www.msn.com';
                }
                else {
                    window.location = 'http://www.msn2.com';
                }
                alert('heyhi');
            }, error: function (j) {
                alert(':(');
            }
    
        });
    
    });
    

    【讨论】:

      猜你喜欢
      • 2011-03-11
      • 2021-06-09
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 2013-03-08
      • 1970-01-01
      相关资源
      最近更新 更多