returning message within data from ajax post

           $.ajax({
                type: "POST",
                url: passUrl + "/RTOSaveJsonData",
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify(DTO),
                dataType: "json",
                async: false,
                success: function (data, textStatus, xmlHttpRequest) {
                    alert('success ');
             alert(data);
                    $("#postResults").html(data);
                },
                error: function(xhr, textStatus, error){
                console.log(xhr.statusText);
                console.log(textStatus);
                console.log(error);
                }

Open in new window


In my webservice, I want to return a message as to if the result was a success or if an error was found. Evidently $.post includes a success callback but not one for errors (according to forum post I read). So I would like to display any errors in the alert or on the screen.

In the above, I want to display the value I return, which is a string value and I assume somewhere within "data" but I don't know how to get the value I returned from within "data" to display to the screen as an alert or displayed on the screen.

I did try the above alert(data); and it just returns Object object.
I tried this:
         $("#postResults").html(data);

and nothing displays in my div tag:
   <div id="postResults"></div>

My message might be something like this: "Failure: you must have a user name."  So it's pretty simple.

Any suggestions for how to get that message out of the data that was returned?

thanks!
LVL 2
Starr DuskkASP.NET VB.NET DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
try:  console.log(data)

Put this as the first line in your success function (for testing only -- remove it after you're done working on this problem).
This will show you the structure of the json object being returned.  You will need to look at the console of your web browser dev tool in order to see the log.  In Chrome, you can right click on the page and select Inspect element, in IE press F12 and in Firefox you can use Firebug.  All of these tools have a tab called console, and that's where you want to look.

We know it's json because you have:

dataType: "json"

P.S.  My guess is that you can access you string by doing alert(data.d), but the console log will confirm the structure of the object.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
Thanks for teaching me how to fish!!!
Best advice all day!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.