lazyPower
lazyPower

Reputation: 267

JQuery and Ajax - Coming to Grips

I've read through several tutorials on the web about ajax posting with JQuery, all of them reference the response object from the web service as response / response.d -- This lead me to believe that this is the built in object for JQuery's response handler.

Code Snippet:

$('.submit').click(function () {
    var theURL = document.location.hostname + ":" + document.location.port + "/LeadHandler.aspx/hello"; // this will change too
    alert(theURL);
    $.ajax({
        type: "POST",
        url: theURL,
        data: "{'NameFirst':'" + $('#txtFirstName').val() + "'}", // again, change this
        contentType: "applications/json; charset=utf-8",
        dataType: "json",
        success: alert("Success: " + response.d), // this will change
        failure: function (response) {
            alert("Failure: " + response.d);
        }
    });
});

however the code is returning "Uncaught ReferenceError: response is not defined" in Chrome's Javascript console. What assumptions am I making that I need to re-evaluate.

Upvotes: 2

Views: 465

Answers (2)

Marco Ceppi
Marco Ceppi

Reputation: 7712

Success (Like Failure) need a function to pass the response object through.

$('.submit').click(function () {
    var theURL = document.location.hostname + ":" + document.location.port + "/LeadHandler.aspx/hello"; // this will change too
    alert(theURL);
    $.ajax({
        type: "POST",
        url: theURL,
        data: "{'NameFirst':'" + $('#txtFirstName').val() + "'}", // again, change this
        contentType: "applications/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            alert("Success: " + response.d);
        },
        failure: function (response) {
            alert("Failure: " + response.d);
        }
    });
});

Upvotes: 3

chprpipr
chprpipr

Reputation: 2039

You need to supply success with a function to execute:

success: function(response) {
    alert(response.d);
}

Upvotes: 6

Related Questions