I’m trying to submit a form using ajax and send the data from a textarea to the body of the post request.
<form method="post">
<textarea name="fkb_newdata" id="fkb_newdata" class="form-control mt-5" cols="30" rows="10">
data...
goes...
here...
</textarea>
<button class="btn btn-primary mt-2" type="submit" id="fkb_savedata">
Daten übernehmen
</button>
</form>
using javascript I prevent the default behaviour of the browser and submit the data to a c# endpoint.
$("#fkb_savedata").on("click", function() {
event.preventDefault();
somefunctiontopreparethedata();
$.ajax({
url: "/backend/apiendpoint",
type: "POST",
data: {
newdata: newdata // JSON.stringify(newdata)
},
dataType: "json",
success: function(res) {
// console.log(res);
$("#fkb_savedata").text("Daten wurden gespeichert");
$("#fkb_savedata").prop("disabled", true);
},
error: function(error) {
console.log('error');
}
});
});
with both data
and newdata
I have the issue that when I inspect with the network tab that the data looks like an array
e.g. for newdata:
Array ( [newdata] => Array ( [0] => Array ( [0] => 001086 [1] => 30.42 ) [1] => Array ( [0] => 001188 [1] => 30.42 ) ) )
and for JSON.stringify(newdata)
Array ( [newdata] => [["001086","30.42"],["001188","30.42"]] )
Formdata in The Payload Tab:
newdata: [["001086","30.42"],["001188","30.42"]]
Is there a simple way to transform this array to propper json format for submitting to the api endpoint?
thank you in advance!