2017-05-27 69 views
0

当我尝试在Web服务中打印接收到的参数时。 该参数为空。 如果我查看玻璃鱼服务器的域服务器的日志,我可以看到下面的 打印:如何使用XMLHttpRequest将参数从javascript传递到REST风格的Web服务

里面的getJSON()
countryKey =

所以我理解请求arruved到Web服务,但是从JavaScript网址发送的参数 是空

// This is the code of the web service method 

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

    return "countryKey = " + countryKey; 
} 

// This is the javascript method 
function populateDistrictList() { 
    var element = document.getElementById("selectCountry"); 
    var selectedCountryKey = element.value; 
    var selectedCountry = element.options[element.selectedIndex].text; 
    var xmlHttp = new XMLHttpRequest(); 
    var url = "http://localhost:8080/MissionWS/webresources/generic?selectedCountryKey="+selectedCountryKey; 
    xmlHttp.open('GET', url, true); 
    xmlHttp.responseType = 'json'; 

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

回答

0

请尝试添加@QueryParam到您的方法签名如下。

public String getJson(@QueryParam("selectedCountryKey") String countryKey) 
相关问题