We help IT Professionals succeed at work.

ajax json javascript

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
The following codes came from mvc action result.
Assume the message return string called "file 1"
Using javascript, how to get value (which is file 1).

Thanks

 if (isSavedSuccessfully)
            {
                return Json(new { Message = fName });
            }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
If you're using jQuery, then the result of the AJAX request will be passed back to the Done event as a parameter, That result will be an Object with properties named according to your code (Message in your example above):

$.ajax({
    // your AJAX Options
    dataType: "json",
    url : "somePage",
    method: "post",
    ...
}).done(function(data) {
    // data will be Object, with properties:
    console.log( data.Message );
});

Open in new window

If you're not using jQuery, the principles will likely be the same - your AJAX request will have an event handler that gets ran when it completes. The result of your code will be passed back to that handler as JSON, so you just access the properties accordingly.

Author

Commented:
file.xhr.responseText = return:  {"Message":"3d8d8436-04e3-4866-aca7-0c9f74ae3012.jpg"} look good
but file.Message = return Undefined.

<script>
                            Dropzone.options.myAwesomeDropzone = {
                                init:
                                    function () {
                                    this.on("complete", function (file) {
                                        alert("Save File:  " + file.xhr.responseText);
                                        alert("Json Return Msg: " + file.Message);
                                        document.getElementById("photolistingtodatabase").value = document.getElementById("photolistingtodatabase").value + file.xhr.responseText + ",";
                                    });
                                }
                            };
                        </script>

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
In your case, the the file parameter is the XMLHttpRequest Object. To get to the actual content, you need to access the responseText property, and probably convert it to JSON yourself:

var myData = JSON.parse(file.xhr.responseText);
alert("Json Return Msg: " + myData.Message);

Open in new window

Author

Commented:
perfect. You are the man!!!

Author

Commented:
if you can help me this one ... it will be very helpful
https://www.experts-exchange.com/questions/29166678/dropzone-add-and-remove-script.html