Question: Consider the code popup.js given below: document.addEventListener('DOMContentLoaded', function() { document.getElementById('searchButton').addEventListener('click', search); }); function search() { var searchTerm = document.getElementById('searchTerm').value; var query = {'searchTerm': searchTerm}; chrome.tabs.query({active:

Consider the code popup.js given below:

document.addEventListener('DOMContentLoaded', function() {

document.getElementById('searchButton').addEventListener('click', search);

});

function search() {

var searchTerm = document.getElementById('searchTerm').value;

var query = {'searchTerm': searchTerm};

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {

var url = tabs[0].url;

var xhr = new XMLHttpRequest();

xhr.open('POST', 'http://localhost:8080/search');

xhr.setRequestHeader('Content-Type', 'application/json');

xhr.onload = function() {

if (xhr.status === 200) {

displayResults(xhr.responseText);

} else {

alert('Error: ' + xhr.statusText);

}

};

xhr.send(JSON.stringify(query));

});

}

function displayResults(results) {

var div = document.getElementById('results');

div.innerHTML = '';

var ul = document.createElement('ul');

JSON.parse(results).forEach(function(result) {

var li = document.createElement('li');

li.textContent = result;

ul.appendChild(li);

});

div.appendChild(ul);

}

Here JavaScript code is responsible for handling the user input, making the AJAX request to the REST API or Servlet, and displaying the search results of a word from a web page.

Question:

how can REST api be coded in order to be called using the url http://localhost:8080/search , explain about the rest api how it coded and called

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!