user1027620
user1027620

Reputation: 2785

alert response from server via jquery

Here's the response:

{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}

how can i display an alert box with the value of "Message". This has not worked for me:

error: function (data) { alert(data[0]); }

or

error: function (data) { alert(data.Message); }

Upvotes: 0

Views: 368

Answers (3)

Jason Meckley
Jason Meckley

Reputation: 7591

try

error: function(header, status, exception) {
    alert(exception.Message);
}

taken from http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings

Upvotes: 2

Neha
Neha

Reputation: 2955

Either you can go with as Christian suggests or use

function (response) {
var error = $.parseJSON(response.responseText);
alert('Sorry, an error occurred. Please contact support. The error was: ' + error.Message);
}
});

Upvotes: 0

DennisZhong
DennisZhong

Reputation: 410

if you has got the data as string,you can use:

var msg = jQuery.parseJSON(data); alert(msg.Message);

Upvotes: 1

Related Questions