Stripping away HTML tags from a JSON output

I'm attempting to retrieve a JSON result using 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);
    }
  }
});

However, the result I am receiving contains HTML tags.

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

Although I am getting the JSON output, it is wrapped in HTML tags. My goal is to strip away the HTML and obtain just the JSON result.

Can anyone assist me with this? Is there something in the dataType or contentType that needs to be adjusted?

Answer №1

To receive the correct response, you need to specify the content type. Instead of requesting HTML format using:

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

You should use the following code to request JSON data:

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

By making this change, you will receive the raw JSON data in return.

Answer №2

Your issue lies within the contentType attribute. Currently, it is configured to text/html; ..., resulting in an html structure being returned. To resolve this, consider either removing the contentType from your request altogether or changing it to application/json; charset=utf-8 for a raw JSON response.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

javascript Incorrectly using location.reload will not refresh the page as intended

My goal is to refresh a page every time a user clicks a button so the page reverts back to its original source code. However, I am facing an issue where the location.reload() function is being executed after the code instead of at the beginning. btn.addEve ...

How to retrieve the user-agent using getStaticProps in NextJS

Looking to retrieve the user-agent within getStaticProps for logging purposes In our project, we are implementing access and error logs. As part of this, we want to include the user-agent in the logs as well. To achieve this, we have decided to use getSta ...

Issue encountered while trying to implement a recursive function for mapping through nested elements was not producing the

I am currently working on recursively mapping through an array of nested objects, where each object can potentially contain the same type of objects nested within them. For example: type TOption = { id: string; name: string; options?: TOption; } con ...

Using Selenium to handle asynchronous JavaScript requests

Having recently started working with Selenium and JavaScript callback functions, I've encountered a problem that I can't seem to solve on my own. My issue revolves around needing to retrieve a specific variable using JavaScript. When I manually i ...

Encountering a problem while attempting to parse a JSON object in Java using the

I am struggling with a JSON string that looks like this: String result={[{"id":"2","fullname":"Course 1"},{"id":"3","fullname":"Course 2"}]} In my Java code, I attempted to decode the JSON string using the following snippet: public class Courses { publ ...

What steps do I need to take in order to modify information within a table using Symfony?

Working on my Symfony project, I frequently need to display various views in table format. I wish I could easily update the data by clicking on table cells, entering new information, and saving it dynamically. After exploring jqGrid, DataTable with jEdit ...

Step-by-step guide on using Ajax to accurately delete selected records with multiple checkboxes

When I utilize a loop to generate rows of data, each row is assigned a unique value for data-host-id as confirmed by the Chrome Developer Tool. However, when clicking on any record, it consistently updates the initial record instead of the intended one. Th ...

Ways to modify CSS using JavaScript

Can anyone assist me with a custom CSS code that I found? It looks like this: header.type-2:not(.fixed-header) nav>ul>li>a{ color:#000; } I've been trying to change the color to #fff using JavaScript, but haven't had any success yet. ...

Shut down a pop-up overlay

I've been facing a challenge in implementing greasemonkey to automatically close a modal when the "x" button is clicked. Allow me to share with you the code snippet for the webpage: <div class="modal-header"> <button type="button" class="clo ...

Transforming lambda expressions into JSON objects using MongoDB .NET Driver

Struggling with mocking frameworks for unit tests, I find it easier to execute queries as strings rather than lambda expressions within my projects. This has led me to consider modifying my IDatabase interface: Interface IDatabase { IEnumerable<Use ...

Use AppScript to exclude the first row (which contains the column names) when filtering a table

Considering the limitations of Google Sheets with the ImportRange function, I decided to develop an AppScript as a replacement. Although I am new to JavaScript, here is what I have so far: function My_ImportRange() { var clearContent = SpreadsheetApp.ge ...

Issue with using Javascript variables within Highcharts

I am facing an issue with displaying a high charts pie chart dynamically. When I pass the exact value format into the data index in the high chart, it doesn't show anything in the chart. However, if I directly assign a value to a variable, it works fi ...

Displaying the current time and total time of a custom video player using Javascript

Currently, I'm in the process of creating an html5 video player and have incorporated javascript to update the current time as a fraction of the total time. The script I've written so far is as follows: function updateTime() { var curTime = ...

Adding a request parameter to an ajax Request for a jQuery dataTable that has been initialized from JSON and mapped by Spring can be achieved by including

I have a jQuery dataTable that is initialized from jSON and mapped by a Spring framework. Currently, I am not passing any parameters and retrieving all information. However, I now need to pass a single String to the Java method, most likely through a requ ...

Display a division upon choosing an option

I am working on a project that involves a selection menu in the form of a drop-down list. <select> <option id="one" value="something">Car</option> <option id="two" value="anything">Plane</option> </select> Also, I ...

Steps for integrating MongoDB with the Ani Meteor Theme

Having some trouble connecting my MongoDB with the Ani Meteor Theme. I attempted to create a package.json with the necessary configurations, but it didn't work out as expected: { "galaxy.meteor.com": { "env": { "MONGO_URL": "mongodb ...

How can we automatically delete a message in DiscordJS after a certain amount of time has passed?

I am inquiring about DiscordJS and have a specific question. My query is regarding how to correctly remove a message sent by the bot on Discord. Admittedly, I am new to DiscordJS so please bear with me if this is a basic question. I appreciate all respo ...

Learning how to invoke a JavaScript function from a Ruby on Rails layout

In the file app/views/download.js.erb, I have defined a javascript function named timeout(). This function continuously polls a specific location on the server to check if a file is ready for download. I am currently running this function as a background ...

When a login attempt is unsuccessful, I am redirected to /api/auth/error using next-auth

Currently, I am utilizing next-auth version 4.18.8 on my login page for the final project of my Fullstack JS course. It's worth noting that a more recent version is being used compared to what was taught in the course (next-auth v. 3). Upon entering ...

Tips for preserving data while attempting to access the schema

Attempting to store data from a book that includes author and genre information, referenced in separate files. The issue arises when making the reference in the main schema. Although the book carries details of the book itself, it fails to establish refer ...