Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on 

ajax json javascript

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

.NET MVCJavaScript

Avatar of undefined
Last Comment
ITsolutionWizard
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of ITsolutionWizard

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ITsolutionWizard

ASKER

perfect. You are the man!!!
Avatar of ITsolutionWizard

ASKER

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
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo