I have created a form with several inputs, I need to pull those values from the inputs and submit to an API using an ajax GET. I am having problems structuring my ajax call and also verifying the call was successful. I have searched and seen "GET"s structured several different ways and need to know which way is proper/ when to use different structures.
below is my form
<form id="target" method="GET" enctype="multipart/form-data" >
<p>First Name: <input class="field" type="text" name="firstName" id="firstname" required></p>
<p>Last Name: <input class="field" type="text" name="lastName" id="lastname" required> </p>
<p>Email: <input class="field" type="email" name="email" id="email" required></p>
<p>Zip Code:<input class="field" type="number" name="zip" id="zip" required></p>
<p class="small"><input type="checkbox" id="privacy" name="privacy" value="Agree" required>I certify that I am a U.S. resident over the age of 18, and I agree to the Privacy Policy</p>
here is my jquery assigning the form values to variables and the ajax call
$(document.ready(function(){ ............
$('#target').submit(function(event) {
// get the form data
var firstName = $('input[name=firstName]').val();
var lastName = $('input[name=lastName]').val();
var email = $('input[name=email]').val();
var zip = $('input[name=zip]').val();
// process the form
$.ajax({
type : 'GET',
url : 'http://test.XXXXXXX.com/x/x/x/x/x/x/.action?source=182081&firstName='+firstName+'&lastName='+lastName+'&email='+email+'&zip='+zip,
dataType : 'json',
success: function(data){
alert('successful');
}
})
.done(function(data) {
console.log(data);
});
event.preventDefault();
$('#banner-expanded').hide();
$('#container1').hide();
$('#thankyou').show();
});
The two main questions I have..
1) Is the ajax call structured properly? should i take the URL and Data and break them up like so?
url : 'http://test.XXXXXXX.com/api/event/form/optinNational.action?source=182081&
data : firstName='+firstName+'&lastName='+lastName+'&email='+email+'&zip='+zip,
2) Besides the "alert" and "console.log" I put, is there any other way to see if the call is successful?
I created a jsfiddle here http://jsfiddle.net/33snB/5/
thanks in advance