javascript - Remove HTML tags in JSON result - Stack Overflow

I am trying to access a JSON result from jQuery.$.ajax({type: "GET",url: "http:localho

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

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/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?

Share Improve this question edited Oct 30, 2017 at 7:55 Shiladitya 12.2k17 gold badges28 silver badges42 bronze badges asked Oct 30, 2017 at 7:51 user2538559user2538559 592 silver badges10 bronze badges 1
  • I guess you will have to do JSON.parse(json.body). Also, you can look into $.getJSON() – Rajesh Commented Oct 30, 2017 at 7:53
Add a ment  | 

2 Answers 2

Reset to default 7

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; ... which returns you the html structure. Try removing the contentType from your request or setting it to application/json; charset=utf-8 to get raw JSON output.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742362174a4429603.html

相关推荐

  • javascript - Remove HTML tags in JSON result - Stack Overflow

    I am trying to access a JSON result from jQuery.$.ajax({type: "GET",url: "http:localho

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信