简体   繁体   English

删除JSON结果中的HTML标签

[英]Remove HTML tags in JSON result

I am trying to access a JSON result from jQuery. 我正在尝试从jQuery访问JSON结果。

$.ajax({
  type: "GET",
  url: "http://localhost:8080/App/QueryString.jsp?Query="+query,
  contentType:"text/html; charset=utf-8",
  dataType: "json",
  success: function(json) {
    if(data!=""){
      console.log(json);
      var data = json.Json;
      console.log(data);
    }
  }
});

But this is giving me result with HTML tags in it. 但这给了我带有HTML标记的结果。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <body> JSON:[Includes the result]</body>
</html> 

I am getting the json output but enclosed with HTML tags. 我正在获取json输出,但随附HTML标记。 I just want to remove them and get the json result only. 我只想删除它们并仅获取json结果。

Can somebody help me on this? 有人可以帮我吗? Does it have something to do with the dataType and contentType? 它与dataType和contentType有关吗?

You use: 你用:

contentType:"text/html; charset=utf-8"

This asks for HTML format. 这要求HTML格式。 Change that to: 更改为:

contentType:"application/json; charset=utf-8"

And you should get the raw JSON back. 并且您应该获取原始的JSON。

The problem is with your contentType property. 问题出在您的contentType属性上。 You have set it to text/html; ... 您已将其设置为text/html; ... text/html; ... which returns you the html structure. text/html; ...会返回html结构。 Try removing the contentType from your request or setting it to application/json; charset=utf-8 尝试从您的请求中删除contentType或将其设置为application/json; charset=utf-8 application/json; charset=utf-8 to get raw JSON output. application/json; charset=utf-8以获取原始JSON输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM