Object's field cannot be accessed after $.ajax call in success function - it is undefined

Posted by dragonfly on Stack Overflow See other posts from Stack Overflow or by dragonfly
Published on 2012-10-28T10:57:38Z Indexed on 2012/10/28 11:00 UTC
Read the original article Hit count: 224

Filed under:
|
|

I have a $.ajax call and and server returns JSON. This is my call:

            var result; // tmp variable just for playing with Chrome console
            $.ajax({
                type: "GET",
                url: "ashxJSON.ashx",
                data: { name: "some sample data" },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    result = msg;
                    console.log(msg);
                    $("#output").append("<p>" + msg.message + "</p>");
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    console.log(textStatus);
                    console.log(errorThrown);
                }
            });

Server returns exactly this string:

{ "messsage": "Hello World to ..."}

And now, when I:

console.log(msg)

I see:

Object messsage: "Hello World to..." proto: Object

But when I access

msg.message

or

msg["message"]

I see:

undefined

What is wrong with me / that msg object?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX