JQuery Form plugin - no data from file upload?
I'm struggling a bit with the jQuery Form plugin. I want to create a file upload form that sends data (JSON from a selected file) to a REST service opened by a servlet. The POST URL is calculated based on what the user selects from the SELECT dropdown. When the download is complete, I want to notify the user immediately, AJAX style.
The problem is that the POST header has content length 0 and contains no data. Any help would be appreciated!
<html>
<head>
<script type="text/javascript" src="js/jquery-1.4.2.min.js">/* ppp */</script>
<script type="text/javascript" src="js/jquery.form.js">/* ppp */</script>
<script type="text/javascript">
function cb_beforesubmit (arr, $form, options) {
// This should override the form action attribute
options.url = "/rest/services/" + $('#selectedaction')[0].value;
return true;
}
function cb_success (rt, st, xhr, wf) {
$('#response').html(rt + '<br>' + st + '<br>' + xhr);
}
$(document).ready(function () {
var options = {
beforeSubmit: cb_beforesubmit,
success: cb_success,
dataType: 'json',
contentType: 'application/json',
method: 'POST',
};
$('#myform').ajaxForm(options);
$.getJSON('/rest/services', function (data, ts) {
for (var property in data) {
if (typeof property == 'string') {
$('#selectedaction').append('<option>' + property + '</option>');
}
}
});
});
</script>
</head>
<body>
<form id="myform" action="/rest/services/foo1" method="POST" enctype="multipart/form-data">
<!-- The form does not seem to submit at all if I don't set action to a default value? !-->
<select id="selectedaction">
</select>
<input type="file" value="Choose"/>
<input type="submit" value="Submit" />
</form>
<div id="response">
</div>
</body>
</html>
+2
a source to share