简体   繁体   中英

Remove HTML tags in JSON result

I am trying to access a JSON result from jQuery.

$.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.

<!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. I just want to remove them and get the json result only.

Can somebody help me on this? Does it have something to do with the dataType and contentType?

You use:

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

This asks for HTML format. Change that to:

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

And you should get the raw JSON back.

The problem is with your contentType property. You have set it to text/html; ... text/html; ... which returns you the html structure. Try removing the contentType from your request or setting it to application/json; charset=utf-8 application/json; charset=utf-8 to get raw JSON output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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