1

I want to get the JSON data from MVC Controller.

NOTE: I am calling Controller through XMLHTTPRequest.Send(data).

The Controller is returning the JSON data, but I don't know how to get that data if we are using XMLHTTPRequest.

I am not using Ajax calls due to some reasons. But is it possible to get it through an XMLHttpRequest or response? If so, kindly help me..

Example for the controller is below. From this, I want to get the FileName and FilePath.

    public JsonResult SaveAttachments()
    {
        try
        {

            .......
                }
            }
            return new Program1.WebClient.App_Start.BaseController.WrappedJsonResult
            {
                Data = new
                {
                    isSuccess = true,
                    CusEvent = "close",
                    CusMsg = "Attachment file upload successful",
                    FileName = fileName,
                    FilePath = filePath
                }
            };
        }
        catch (Exception ex)
        {
            throw new Exception("File upload failed. ");
        }

    }
Vikash
  • 39
  • 1
  • 2
  • 8
  • Maybe answer on [this question](http://stackoverflow.com/questions/247483/http-get-request-in-javascript) can help you? – Andrew Orlov Aug 21 '15 at 09:32
  • with a simple search http://stackoverflow.com/questions/22528696/return-json-string-from-mvc-controller – Luca Aug 21 '15 at 09:34

1 Answers1

0

You can try with XMLHttpRequest.responseText and manipulate on it.

Quan Nguyen
  • 562
  • 1
  • 5
  • 20
  • Actually, I have not used this XMLHttpRequest before. I want to know whether the responseText will get the Json returned from the MVC Controller? – Vikash Aug 21 '15 at 09:34
  • @Vikash you would probably need to manually parse the string using `JSON.parse` method – Vsevolod Goloviznin Aug 21 '15 at 09:35
  • xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { var myArr = JSON.parse(xhr.responseText); } } I tried like the above.. It throws an error in the console saying that Uncaught InvalidStateError: Failed to read the 'responseText' property from 'XMLHttpRequest': The value is only accessible if the object's 'responseType' is '' or 'text' (was 'json'). – Vikash Aug 21 '15 at 09:36
  • 1
    before you call XMLHTTPRequest.Send(data), try to set type of responseType prop is json like this: XMLHTTPRequest.responseType = "json", after receiving data, then use JSON to parse it. Tell me it if it works. – Quan Nguyen Aug 21 '15 at 09:53