# Passing Argument to Web Service

**URL:** <https://forum.kirupa.com/t/passing-argument-to-web-service/636688>\
**Category:** programming\
**Created:** [May 26, 2017, 12:46pm UTC](https://forum.kirupa.com/t/passing-argument-to-web-service/636688 "2017-05-26T12:46:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dror](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@dror](https://forum.kirupa.com/u/dror)\
**Post date:** [May 26, 2017, 12:46pm UTC](https://forum.kirupa.com/t/passing-argument-to-web-service/636688/1 "2017-05-26T12:46:00Z")

</div>

How can i pass parameter to Restfull web service using XMLHttpRequest in javascript.

I have the following web service method:

```auto
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String getJson(String countryKey) {
        System.out.println("Inside getJson()");
        System.out.println("countryKey = " + countryKey);
        
        return "countryKey = " + countryKey;
    }

```

and I’m trying to call this method from javascript as follow:

```auto
function populateDistrictList() {
                var element = document.getElementById("selectCountry");
                var selectedCountryKey = element.value;
                var selectedCountry = element.options[element.selectedIndex].text;
                alert(selectedCountryKey + " - " + selectedCountry);
                var xmlHttp = new XMLHttpRequest();

                var url = "http://localhost:8080/MissionWS/webresources/generic?
               selectedCountryKey="+selectedCountryKey;
                alert("url = " + url);
                //xmlHttp.open('GET', url, true);
                xmlHttp.open('GET', url, true);
                xmlHttp.responseType = 'json';

                if (xmlHttp) {
                    xmlHttp.onreadystatechange = function () {
                        if (xmlHttp.readyState == 4) {
                            if (xmlHttp.status == 200) {
                                var text = xmlHttp.responseText;
                                alert(text);
                            } else {
                                alert("Something is wrong !");
                            }
                        }
                    };
                    xmlHttp.send();
                }
            }

```

I get an empty string  
Can someone help ???

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [May 31, 2017, 3:51pm UTC](https://forum.kirupa.com/t/passing-argument-to-web-service/636688/2 "2017-05-31T15:51:43Z")

</div>

If you manually construct the URL (aka hardcode the argument), does the correct value get returned?
