jQuery - ajax不发送请求

时间:2013-11-20 16:17:00

标签: jquery ajax

我有以下代码:

$("#profileImageButton").click(function () {
    var id = this.id;
    var cid = id.split('_')[1];

    $.ajax(
    {
        type: "GET",
        url: "/Profile/SetProfile",
        dataType: "json",
        data: { id: cid },
        async: true,
        beforeSend: function () {
            alert("BEFORE!");
        },
        complete: function (data) {
            $(".bodyContentNotification").show();
            $(".bodyContentNotification").text(data.responseJSON.msg);
            $(".bodyContentNotification").attr("id", "WarningNotification");
        }
    });
});

这是极其简单的代码,但有些不对劲。当我点击按钮时,chrome调试器会在beforeSend上捕获破坏点(并且确定,我发出警告消息),消息显示并且没有任何反应。 Fiddler没有捕获任何事件,我认为,ajax()不会发送请求。我的代码出了什么问题?

2 个答案:

答案 0 :(得分:3)

你有错误!

您的目标是ID为profileImageButton的元素 在第一行,您正在this.id,这仍然是profileImageButton 在您使用id.split('_')[1];之后的下一行,但该数组中没有[1],因为该ID中没有下划线,这是一个错误。

答案 1 :(得分:0)

不要尝试在论坛标记内放置jQuery发送按钮。我在代码中写了这样的东西:

<form action="..." method="POST">
    ...
    <button id="profileImageButton"></button>
</form>

profileImageButton - 点击此触发点击事件,我尝试发送ajax get请求。这是错误的把它放在形式上。

相关问题