After making an ajax call below, I encountered an error that left me puzzled. The variable 'response' in the success function is structured as follows:
{"status": "complete", "username": "test", "error": "0", "message": ""}
Surprisingly, when I attempted to display the 'error' key using alert functions within the success function, the first one worked fine but the next two returned 'undefined'. Even though I am aware of the existence of the error key, JavaScript seems unable to recognize it which ultimately led to a crash.
$.ajax({
url: '/index.php/api/userLogin',
data: userInfo,
datatype: 'json',
async: 'false',
type: 'POST',
success: function(response)
{
alert(response); //prints correct response
alert(response.error); //prints undefined
alert(response["error"]); //prints undefined
},
error: function(xhr, status, error)
{
var err = eval("(" + xhr.responseText + ")");
//alert("Please Try Again, we had an internal error!");
alert(err.message);
}
});
If anyone can shed some light on what might be causing this issue and offer a solution, it would be greatly appreciated.