I'm trying to post some form data using javascript against a Jersey Resource. Here is the javascript:
var form = document.getElementById('form');
var formdata = new FormData(form);
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("xmlTextBox").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST", "PostXml", true);
xmlhttp.setRequestHeader('Content-Type', 'multipart/form-data');
xmlhttp.setRequestHeader("Content-length", formdata.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(formdata);
The Jersey Resource look like this:
@Path("/Resource")
public class MyClass {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_XML)
public String postXML(@FormDataParam("param1") String param1,
@FormDataParam("param2") String param2){
return "test";
}
The real version includes more params and full code, but the annotations are the same. This produces the following exception when running through tomcat:
java.lang.NullPointerException
at .sun.jersey.multipart.impl.MultiPartReaderClientSide.unquoteMediaTypeParameters(MultiPartReaderClientSide.java:227)
at .sun.jersey.multipart.impl.MultiPartReaderClientSide.readMultiPart(MultiPartReaderClientSide.java:154)
at .sun.jersey.multipart.impl.MultiPartReaderServerSide.readMultiPart(MultiPartReaderServerSide.java:80)
at .sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:144)
at .sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:82)
at .sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:488)
at .sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:552)
From looking at the source that produced the exception it looks like the param isn't ing through:
224 for (final String parameterName : parameters) {
225 String parameterValue = mediaType.getParameters().get(parameterName);
226
227 if (parameterValue.startsWith("\"")) {
228 parameterValue = parameterValue.substring(1, parameterValue.length() - 1);
229 unquotedParams.put(parameterName, parameterValue);
230 }
231 }
I;ve used firebug to put a trace on and the name / values are ing through differently when using javascript pared to a straight HTML post. In the trace for the HTML post the content type is returned in an upload stream:
Request Headers From Upload Stream
Content-Length 1756
Content-Type multipart/form-data; boundary=---------------------------1523409566516443041527622966
But the javascript seems to be just a standard post or something? Any ideas how I replicate the multiformdata post in javascript??
Any ideas as it looks like i'm passing things through OK? I've also tried this using a normal HTML form post and it works fine, so must be related to the javascript.
I'm trying to post some form data using javascript against a Jersey Resource. Here is the javascript:
var form = document.getElementById('form');
var formdata = new FormData(form);
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("xmlTextBox").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST", "PostXml", true);
xmlhttp.setRequestHeader('Content-Type', 'multipart/form-data');
xmlhttp.setRequestHeader("Content-length", formdata.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(formdata);
The Jersey Resource look like this:
@Path("/Resource")
public class MyClass {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_XML)
public String postXML(@FormDataParam("param1") String param1,
@FormDataParam("param2") String param2){
return "test";
}
The real version includes more params and full code, but the annotations are the same. This produces the following exception when running through tomcat:
java.lang.NullPointerException
at .sun.jersey.multipart.impl.MultiPartReaderClientSide.unquoteMediaTypeParameters(MultiPartReaderClientSide.java:227)
at .sun.jersey.multipart.impl.MultiPartReaderClientSide.readMultiPart(MultiPartReaderClientSide.java:154)
at .sun.jersey.multipart.impl.MultiPartReaderServerSide.readMultiPart(MultiPartReaderServerSide.java:80)
at .sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:144)
at .sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:82)
at .sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:488)
at .sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:552)
From looking at the source that produced the exception it looks like the param isn't ing through:
224 for (final String parameterName : parameters) {
225 String parameterValue = mediaType.getParameters().get(parameterName);
226
227 if (parameterValue.startsWith("\"")) {
228 parameterValue = parameterValue.substring(1, parameterValue.length() - 1);
229 unquotedParams.put(parameterName, parameterValue);
230 }
231 }
I;ve used firebug to put a trace on and the name / values are ing through differently when using javascript pared to a straight HTML post. In the trace for the HTML post the content type is returned in an upload stream:
Request Headers From Upload Stream
Content-Length 1756
Content-Type multipart/form-data; boundary=---------------------------1523409566516443041527622966
But the javascript seems to be just a standard post or something? Any ideas how I replicate the multiformdata post in javascript??
Any ideas as it looks like i'm passing things through OK? I've also tried this using a normal HTML form post and it works fine, so must be related to the javascript.
Share Improve this question asked May 13, 2013 at 21:46 thomascrabsthomascrabs 731 silver badge6 bronze badges1 Answer
Reset to default 6Remove those setRequestHeader
var form = document.getElementById('form');
var formdata = new FormData(form);
var xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("xmlTextBox").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST", "PostXml", true);
xmlhttp.send(formdata);
http://jsfiddle/8NWB7/ working
http://jsfiddle/8NWB7/1/ not working
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745351992a4623888.html
评论列表(0条)