最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Json result wrapped in pre tag - how to get value of it - Stack Overflow

programmeradmin4浏览0评论

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>"

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>"
Share Improve this question asked Sep 26, 2011 at 18:22 11101110 6,82956 gold badges186 silver badges346 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 13

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.

发布评论

评论列表(0)

  1. 暂无评论