I have to post data from an html form to an api. I have the following code:
<form name="newAppform">
<input type="text" name="appname" placeholder="App Name" id="appname"><br>
<input type="radio" name="platform" value="iOS"> iOS
<input type="radio" name="platform" value="Android"> Android<br>
<input type="submit" value="submit" name="submit" id="submit"></form>
and the jQuery is:
var a = 1; //var x = $('#appname').val();
$('#submit').click(function(){
//alert($('#appname').val());
$.ajax({
type: 'POST',
url: 'https://service.../.../createApp',
contentType: "application/json",
data: JSON.stringify({"account_id" : a, "app_name" : $('#appname').val(), "app_platform" : $("[name='platform']").val()}),
success: function() {
alert("Complete!");
},
error: function(e) {
console.log(e);
alert("Something went wrong.");
}
});
});
I am using a valid for posting url, which I have replaced with ellipsis here. I am getting the alert 'Something went wrong.'. The error that is getting logged on the console is
{readyState: 0, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}.
What is wrong? Please note that the same posting is working with the same code and I am getting the success message if I am not taking it from form input. Conversely, the form input reading is working separately apart from posting. If I uncomment 'alert' it is being able to display the value I put in the input text field. Please also note that if I uncomment and use the variable x as the value for the key "appname", it is still not working. Same error. But if I hardcode x outside as var x = 'abc', I get success.
stringifythe data propertyerrorhandler logic isn't very helpful - could you check the network tab of the console to see what the response from the request is.