Javascript Form Post with Jersey MultiFormData - Null Pointer Exception -
i'm trying post form data using javascript against jersey resource. here javascript:
var form = document.getelementbyid('form'); var formdata = new formdata(form); if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code 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 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 , full code, annotations same. produces following exception when running through tomcat:
java.lang.nullpointerexception @ com.sun.jersey.multipart.impl.multipartreaderclientside.unquotemediatypeparameters(multipartreaderclientside.java:227) @ com.sun.jersey.multipart.impl.multipartreaderclientside.readmultipart(multipartreaderclientside.java:154) @ com.sun.jersey.multipart.impl.multipartreaderserverside.readmultipart(multipartreaderserverside.java:80) @ com.sun.jersey.multipart.impl.multipartreaderclientside.readfrom(multipartreaderclientside.java:144) @ com.sun.jersey.multipart.impl.multipartreaderclientside.readfrom(multipartreaderclientside.java:82) @ com.sun.jersey.spi.container.containerrequest.getentity(containerrequest.java:488) @ com.sun.jersey.spi.container.containerrequest.getentity(containerrequest.java:552)
from looking @ source produced exception looks param isn't coming through:
224 (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 put trace on , name / values coming through differently when using javascript compared straight html post. in trace html post content type returned in upload stream:
request headers upload stream content-length 1756 content-type multipart/form-data; boundary=---------------------------1523409566516443041527622966
but javascript seems standard post or something? ideas how replicate multiformdata post in javascript??
any ideas looks i'm passing things through ok? i've tried using normal html form post , works fine, must related javascript.
remove 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.net/8nwb7/ working
http://jsfiddle.net/8nwb7/1/ not working
Comments
Post a Comment