Jakob Plenge Jakob Plenge - 6 months ago 22
Javascript Question

jQuery Ajax call not working in IE, JSON respond not getting through

I am trying to make an ajax call with a json respond to work in IE, but nothing is happening. It is currently working fine in other browsers.

I have read a lot of places, that there are some general issues with ajax and IE, but none of the solutions have helped.

I have also tried using the $.ajaxSetup({ cache: false }); but that doesn't fix it, it does not seem to have anything to do with caching.

Any help i highly appreciated.

The code is pasted under.

JS:

$(document).on("submit", ".loginForm", function(event) {

event.preventDefault();

var url=$(this).attr("action");
$.ajax({
url: url,
type: 'POST',
data: new FormData(this),
processData: false,
contentType: false,
datatype: "json",
success: function (data, status)
{
if(data.success == "success"){
window.location.replace("dashboard.php");
} else {
$('#loginResponse').html(data.success).css("color", "red").fadeIn().delay(2000).slideToggle();
}
}
});


});

PHP:

header("Content-Type: application/json", true);

if(isset($_POST['submit_login'])) {

$username = $_POST['username'];
$password = $_POST['password'];

$sql = 'SELECT user_id, first_name, password FROM arbor_admin_users WHERE username=?';

$stmt = $connection->prepare($sql);
$stmt->bind_param('s', $username);
$stmt->bind_result($uid, $fn, $pwhash);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0) {
while($stmt->fetch()){


// if ($password === $pw) {
if (password_verify($password, $pwhash)) {
session_start();
$_SESSION['logged_in'] = $uid;
$_SESSION['firstname'] = $fn;
// header("Location: ../dashboard.php");
echo json_encode(array('success'=>"success"));
exit();

} else {

echo json_encode(array('success'=>"Wrong username or password!"));

}
}
} else {
echo json_encode(array('success'=>"Wrong username or password!"));
}
}

Answer

After a long time debugging, i found out that

    data: new FormData(this),

doesn't include the submit button in IE, but does in all other browsers.

So to fix it, i just removed:

  if(isset($_POST['submit_login'])) {

and problem solved :)