Jquery - Ajax:获取发布"数据"内部"错误"

时间:2014-05-22 08:13:35

标签: javascript jquery ajax

小问题(解决方案似乎很简单,但我似乎无法在谷歌上找到它)。我想在data内使用error: function - 设置。我知道这听起来很神秘但允许我举个例子。

我有以下Ajax-Call(Jquery)。

$.ajax({
    type: "GET",
    url: "Search.aspx",
    data: {
        action: 'GetUsers',
        userSearchString: $("#txtUserSearchText").val(),
        Docbase: docbase
    },
    success: function (data) {

        .... (Do something when successfull)

    },
    error: function (xhr, errStatus, thrownError) {

        // RIGHT HERE, i want to use action,userSearchString,Docbase that were passed to Search.aspx 
        // Prefferably i would like to use the data as a whole object and pass it too LogException

        $("#FindUsersModal").modal("hide")
        LogException(errStatus, thrownError, "SearchUsersByInput", params);
    }

})

有人会关心我指向正确的方向吗?

Thnx适合您的时间。

2 个答案:

答案 0 :(得分:1)

为什么不在函数级别创建数据作为对象,然后从错误回调中访问它?例如:

function foo(){

    var jsonData = {
            action: 'GetUsers',
            userSearchString: $("#txtUserSearchText").val(),
            Docbase: docbase
        }

    $.ajax({
        type: "GET",
        url: "Search.aspx",
        data: jsonData,
        success: function (data) {

            .... (Do something when successfull)

        },
        error: function (xhr, errStatus, thrownError) {

            // RIGHT HERE, i want to use action,userSearchString,Docbase that were passed to Search.aspx 

            var something = jsonData.action;        

            $("#FindUsersModal").modal("hide")
            LogException(errStatus, thrownError, "SearchUsersByInput", params);
        }

    })

}

答案 1 :(得分:0)

这里的想法很简单,只需将数据存储在函数范围之外的变量中,您就可以在函数中的任何位置使用它。

这是你需要做的事情

var myData = {
        action: 'GetUsers',
        userSearchString: $("#txtUserSearchText").val(),
        Docbase: docbase
    }

$.ajax({
    type: "GET",
    url: "Search.aspx",
    data: myData,
    success: function (data) {

        .... (Do something when successfull)

    },
    error: function (xhr, errStatus, thrownError) {

        console.log(myData);
        //You can perform any action with myData here
    }

})