Skip to main content

Trouble reading params from Ajax request using RestEasy

No replies
walkernvs
Offline
Joined: 2010-01-26

This may be more of an Ajax question than a Java/REST question, but hopefully someone can make a suggestion. I'm trying to call a RestEasy service via an Ajax form. I've tried hitting this service via a vanilla (non-Ajax) web form and everything works fine. However, when I hit it through an Ajax request, the method within the service fires but is unable to read any of the params. I think this may have something to do with the request header type, as when I explicitly declare the content type (i.e., xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"), the method never even fires. I'm running everything through some pretty standard Ajax/RestEasy calls and the web service is running on WebLogic 10. Code is pasted below, any suggestions would be welcome.

Thanks,
Alex

**************************************************************************
RestEasy method - this does fire, but all params are NULL
**************************************************************************

/**
*
* @param protnumber
* @param region
*/
@Path("/")
@POST
public void checkProtNumber(@FormParam("protnumber") String protnumber, @FormParam("region") String region) {
System.out.println("In checkProtNumber ... protnumber = " + protnumber + ", region = " + region);
// some code to process params...
}

*****************************
ajax call from JS file
*****************************

function runAjaxRest(myURL)
{

var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}

var parameter = encodeURI ("protnumber=" + document.getElementById ("protnumber").value +
"&region=" + document.getElementById ("region").value
);
alert('parameter = ' + parameter);
xmlHttp.open("POST",myURL,true);

xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4 && xmlHttp.status == 200)
{

alert('hooray! It worked! ' );
}
}

//Send the proper header information along with the request
xmlHttp.send(parameter);
}