I'm having difficulty working out why my AJAX code is being sent twice on form submit.
OPTIONS request and a POST request - both 200 status messages. The POST request is successful and returns what I want so that is all good. I do not want the OPTIONS request to occur.
Thought it might have something to do with CORS or unbinding the submit event handler?
If anyone can help that would be great.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form id="loginform">
Username: <input type="text" class="rname" name="username" value=""/>
Password: <input type="text" class="rpass" name="password" value=""/>
<input type="submit" value="Submit" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
scripts = function () {
var _t = this;
var authenticateUrl = "http://...";
this.events = function () {
$('#loginform').submit(_t.loginEvent);
};
this.loginEvent = function (event) {
event.preventDefault();
var $form = $(this);
var _name = $form.find(".rname").val();
var _pass = $form.find(".rpass").val();
var submitData= {username: _name,password: _pass};
submitData = JSON.stringify(submitData);
$.ajax({
type: 'POST',
contentType: "application/json",
dataType: 'json',
url: authenticateUrl,
data: submitData,
success: function(data) {
console.log("Success loginEvent");
if (data.error) {
console.log("Unsuccessful Login");
} else {
console.log("successful Login");
}
}
});
};
this.init = function () {
_t.events();
};
this.init();
return (this);
};
var LZ = new scripts();
</script>
</body>
</html>