0

I have the following class:

[DataContract]
public class FileUploaderResult
{
    [DataMember]
    public bool Success { get; set; }
    [DataMember]
    public string TempFileName { get; set; }

    public FileUploaderResult(bool success, string fileName, string errorMessage)
    {
        this.Success = success;
        //this.RelativeThumbnailUrl = fileName;
        //this.ErrorMessage = errorMessage;
    }
}

and following controller method:

    public JsonResult UploadFileAjax()
    {
        var result = new FileUploaderResult(false, string.Empty, string.Empty);
        try
        {
            //...
            result.Success = true;
            result.TempFileName = filename;
            return Json(result);
        }
        catch
        {
            result.Success = false;
            return Json(result);
        }
    }

on client part I try to parse this JSON :

                var objResponse = jQuery.parseJSON(response);
                alert(objResponse.TempFileName);

and it does not work. When I try to show response as is:

                alert(response);

I got the message with "litter" (

http://ru.magicscreenshot.com/jpg/e8f4D1ciAU8.html

why parseJSON does not work and how to do correctly?

1
  • could you show ur jquery code?? Commented Aug 13, 2011 at 5:23

1 Answer 1

4

That is because you are requesting the data as HTML, not plain text or JSON. The browsers wraps the text in the response in a pre tag to make an HTML page out of it.

When you request the data, specify the data type "json", then the browser will not try to make HTML out of it, and jQuery will parse it for you and return an object.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.