Question

I am returning a simple data to ajax call. But I trying show that returned in alert is showing undefined. How can I get return JSON data?

   return Json(new { message = "success", url = Url.Action("Index", "Image") },
JsonRequestBehavior.AllowGet);

view

$(document).ready(function() {
                $('#fileupload').fileupload({
                    dataType: 'json',
                    url: '/Admin/Image/UploadFiles',
                    autoUpload: true,
                    done: function (e, data) {
                        alert(data.message); //showing undefined
                        if (data.message== 'success') {
                            alert(data.message);
                            window.location = "www.google.com";
                        }
                    },
                    fail: function(e, data) {
                        console.write(data.errorThrown);
                    }
                })
Was it helpful?

Solution

Do you want the complete or success property? You are confusing the xhr object's .done() with ajax setup property complete: or success: I think.

Done's second parameter is a status, not the data.

Try this instead

 $(document).ready(function() {
            $('#fileupload').fileupload({
                dataType: 'json',
                url: '/Admin/Image/UploadFiles',
                autoUpload: true,
                success: function (data, status, xhr) {
                    alert(data.message); 
                    if (data.message== 'success') {
                        alert(data.message);
                        window.location = "www.google.com";
                    }
                }
            })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top