This is first time that I work with json. I am trying to return Json from my action method:
public JsonResult Upload()
{
...
return开发者_运维问答 Json(new { foo = "sos....sos....sos..."});
}
But in result all I get is my message wrap in this "pre" tag. How to parse "foo" from this?
"<pre style="word-wrap: break-word; white-space: pre-wrap;">{"foo":"sos....sos....sos..."}</pre>"
I think the reason you are receiving the data wrapped in a pre tag is because you are requesting the data as HTML and not plain text or json.
Try specifying the data type as json to stop the response being converted to HTML.
This returns the content of the first pre tag with the class "yourclass".
document.querySelector("pre.yourclass").innerHTML
It's possible that the handler on the client side is trying to ensure that the server's response is well formed HTML. I believe some javascript libraries with file upload support will wrap non-HTML repsonses in the tag as you describe. A very non-intuitive solution might be to set the mimetype at the server to be "text/html", so that the ajax handler doesn't try to wrap the response.
精彩评论