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. ");
        }

    }
2

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

4 Comments

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 you would probably need to manually parse the string using JSON.parse method
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').
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.