The goal is to successfully POST a new job to the marathon REST API. I'm using jQuery v1.12.4
The following code is getting values from a form and printing valid json to the console.log. I've verified it using JSONLint. But it doesn't seem to be getting to the server. here's the JSON that's being printed to the log, and that I see in firebug:
{"id":"basic-0","cpus":"0.1","mem":"32","instances":"1","cmd":"while [ true ] ; do echo 'Hello Marathon' ; sleep 5 ; done"}
I'm getting 400 Bad Request, and then I see this crazy response (in firebug):
{"message":"Invalid JSON","details":[{"path":"/instances","errors":["error.expected.jsnumber"]},{"path"
:"/cpus","errors":["error.expected.jsnumber"]},{"path":"/mem","errors":["error.expected.jsnumber"]}]
}
I have no idea what that response means. Is it coming from jQuery or the server? Does anyone know of a working example of calling the marathon REST API from javascript?
Here's the code. Yes, I know it's really bad practice to pass the username and password in the request, but I haven't figured out how to use the basic authentication yet, not have I yet figured out how to enable SSL on the marathon webserver. Baby steps...
var $form = $('#appsubmit1');
$form.submit(function() {
var form2json = JSON.stringify($('form').serializeObject());
console.log(form2json);
$.ajax({
type: 'POST',
url: 'https://user:pass@mesos-head.achillesv.net/marathon/v2/apps',
data: form2json,
dataType: 'json',
contentType: "application/json; charset=utf-8",
timeout: 10000
}).done(function(resp, status, xhr) {
console.log("ajax function done: ", status);
}).fail(function(xhr, status, errmsg) {
console.log("error: ", status);
console.log("error: ", errmsg);
});
return false;
});
Here's the code for the serializeObject function, which I did not write, but found found online. It works great, except that it double quotes all the values. I need the numeric values to be un-quoted. Unfortunately I don't see where the code puts the double quotes in, therefore I don't how to take them out. I'm still working on it.
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
I'm thinking of checking each this.value against a regexp that removes the quotes if the value is completely numeric, i.e., 0.1, 3, 200.5, etc. I'll post that when I figure it out because I figure somebody else must have this same problem.