eagor
eagor

Reputation: 10035

How to access response of ajax request, fired by clicking on a link?

I have a link, clicking on which an ajax POST request is fired up. I do this:

$('a#create_object').click();

This fires an ajax request. The code for this ($.ajax({...})) is written somewhere in bootstrap libs and I do not want to edit thwem.

How do I access the response of the request after ajax successes?

Upvotes: 1

Views: 113

Answers (2)

Flo Schild
Flo Schild

Reputation: 5294

The direct callbacks are deprecated since jQuery 1.8.0, but you can use the .done, .fail and .always callbacks !

And you have to overwrite / access the callbacks if you want to do some stuff, you cannot access it from external I mean !

$('#link').on('click', function(e) {
    $.ajax({
        type: "post",
        url: "your-page",
        data: {},
        dataType: "json"
    }).done(function (response) {
        alert('Success');
        console.log(response); // Use it here
    }).fail(function (response) {
        alert('Fail');
        console.log(response); // Use it here
    }).always(function (response) {
        // Here manage something every-time, when it's done OR failed.
    });
});

Upvotes: 1

Deep Sharma
Deep Sharma

Reputation: 3473

$('#controlId').click(function(){ $.ajax({
        type: "POST",
        url: "PageUrl/Function",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
        alert(response.d); //here you can get the response on success of function
        }
    });
});

Upvotes: 0

Related Questions