I have an AJAX request to send data to an API and receive a response. I would like to know how to dynamically pass the value from an input field to the body data in the AJAX request.
Here is my form:
<input type="text" name="serial" />
<input type="submit" />
Here is my AJAX request:
$.ajax({ url: "http://www.example.com/api",
beforeSend: function(xhr)
{ xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password")); },
type: 'POST',
dataType: 'json',
contentType: 'application/json',
processData: false,
data: '{"serial":"252625"}',
success: function (data) { alert(JSON.stringify(data)); },
error: function(){
alert("Cannot get data"); }
});
I am looking for a way to programmatically replace the value in data: '{"serial":"252625"}'
with the one entered in the serial input field.
Thank you for any suggestions,