I want to submit the contents of a form to an API and parse the response. Currently I am receiving an error from the API as some of the values need to be integers but the request is currently a string.
The format I need my JSON in is:
{ "request": { "slice": [ { "origin": "LAX", "destination": "BOS", "date": "2015-09-09" } ], "passengers": { "adultCount": 1 }, "solutions": 1, "refundable": false } };
However, currently the JSON I am submitting looks like this:
{"request":{"slice":[{"origin":"LAX","destination":"BOS","date":"2015-09-09"}],"passengers":{"adultCount":"1"},"solutions":"1"}}
How do I remove the quotes around the values for keys adultCount and solutions? I don't want to strip the whole string, just the two specific values.
My form looks like this:
<form id="request" action="" onsubmit="request" method="post">
<input type="text" name="request[slice][0][origin]" placeholder="From">
<br>
<input type="text" name="request[slice][0][destination]" placeholder="Destination">
<br>
<input type="text" name="request[slice][0][date]" placeholder="Outbound Date">
<br>
<input type="number" name="request[passengers][adultCount]" placeholder="Passengers">
<br>
<input type="number" name="request[solutions]" placeholder="Results">
<br>
<p><input type="submit" /></p>
</form>
The script I use to stringify the serialised object is:
<script>
var data = $('form#request').serializeObject();
$(function request() {
$('form').submit(function() {
$('#result').text(JSON.stringify($('form').serializeObject()));
return false;
});
});
</script>
I submit this in an AJAX request using this code:
<script>
function request(){
$.ajax({
url:"http://ift.tt/1ydFErh",
type:"POST",
data: JSON.stringify(data),
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(response,status){document.getElementById("response").innerHTML =
JSON.stringify(response);
}
})
};
</script>
Aucun commentaire:
Enregistrer un commentaire