Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7024445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:50:25+00:00 2026-05-27T23:50:25+00:00

i make a simple REST web service consumer using HTML and javascript. here’s the

  • 0

i make a simple REST web service consumer using HTML and javascript. here’s the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script language="javascript">
    var xmlhttp;

    function getdetails() {
        var empno = document.getElementById("empno");
        var url = "http://localhost:8080/TestWS1/rest/hello/" + empno.value;

        xmlhttp = new XMLHttpRequest(); //@slaks: i put it here

        xmlhttp.open('GET',url,true);
        xmlhttp.send(null);

        xmlhttp.onreadystatechange = function() {

               var empname =  document.getElementById("empname");
               var age =  document.getElementById("age");
               if (xmlhttp.readyState == 4) {
                    //alert(xmlhttp.status);
                  if ( xmlhttp.status == 200) {
                       var det = eval( "(" +  xmlhttp.responseText + ")");
                       if (det.age > 0 ) {
                          empname.value = det.name;
                          age.value = det.age;
                       }
                       else {
                           empname.value = "";
                           age.value ="";
                           alert("Invalid Employee ID");
                       }
                 }
                 else
                       alert("Error ->" + xmlhttp.responseText);
              }
        }
    }
  </script>
  </head>
  <body>
   <h1>Call Employee Service </h1>
   <table>
   <tr>
       <td>Enter Employee ID :  </td>
       <td><input type="text" id="empno" size="10"/>  <input type="button" value="Get Details" onclick="getdetails()"/>
   </tr>
   <tr>
       <td>Enter Name :  </td>
       <td><input type="text" readonly="true" id="empname" size="20"/> </td>
   </tr>

   <tr>
       <td>Employee Age : </td>
       <td><input type="text" readonly="true" id="age" size="10"/> </td>
   </tr>
   </table>
  </body>
</html>

that code only show an employee name and age from the REST web service on the HTML textbox when a getDetail button is pressed. the parameter is employee number (empNo).

the main problem is, why this code only works once??

for example, if i put 1 on the empNo textbox and i pressed getDetail button, for the first time only, it will display the name and the age of the employee based on employee number that i was entered before. but for the second or third i press the getDetail button, it not works anymore. i’ve tried to give some alert to help me for debugging that code but the result is xmlhttp.onreadystatechange = function() only works once on the first time i pressed the getDetail button.

has anyone know how to solve this problem?? really stuck in here.. thanks a lot for helping me..

FYI: here’s my web service code:

package com.webservices.TestWS1;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

//@Path("/hello")
@Path("/hello/{empno}")
public class Hello {        
    @GET   // this method process GET request from client
    @Produces("application/json")   // sends JSON
    public String getJson( @PathParam("empno") int empno) {  // empno represents the empno sent from client   
        switch(empno) {
          case 1 :
              return "{'name':'George Koch', 'age':58}";
          case 2:
              return "{'name':'Peter Norton', 'age':50}";
          default:
              return "{'name':'unknown', 'age':-1}";
      } // end of switch
   } // end of
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:50:26+00:00Added an answer on May 27, 2026 at 11:50 pm

    Put this between the script tag

    function getdetails() {
            xmlhttp = new XMLHttpRequest();
            var empno = document.getElementById("empno");
            var url = "http://localhost:8080/TestWS1/rest/hello/" + empno.value;
            xmlhttp.open('GET',url,true);
            xmlhttp.send(null);
            xmlhttp.onreadystatechange = function() {
    
                   var empname =  document.getElementById("empname");
                   var age =  document.getElementById("age");
                   if (xmlhttp.readyState == 4) {
                        //alert(xmlhttp.status);
                      if ( xmlhttp.status == 200) {
                           var det = eval( "(" +  xmlhttp.responseText + ")");
                           if (det.age > 0 ) {
                              empname.value = det.name;
                              age.value = det.age;
                           }
                           else {
                               empname.value = "";
                               age.value ="";
                               alert("Invalid Employee ID");
                           }
                     }
                     else
                           alert("Error ->" + xmlhttp.responseText);
                  }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll make it simple, here's the code I've got. public ActionResult DeleteNonCIStaffUser(int id) {
Ho to make this simple xml with php using DOM? Full code will be
i'm new in a web service. i'm trying to make a simple web service
I am trying to make a simple REST Service with .NET 4.0 that uses
I am trying to make a simple rest api with rails using the respond_with
I'm wanting to make an API quickly, following REST principles - for a simple
I'm working on some WCF Rest services, following some tutorials to make some simple
i am using following code to make an httpPost call but it is returning
I'm trying to get the sample code from Mozilla that consumes a REST web
I'm out to create a fairly simple web service for intranet use. The service

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.