Failed to get response from ajax post request using slim framework 3 (allowed)
Edit:
The problem has been fixed. Special thanks to ymz for taking his time to help me. the localhost address for the ajax request should have been 127.0.0.1 and not 121.0.0.1
on my client i have the following code for POST request to localhost / login
<html>
<title> Welcome </title>
<script>
function makeRequestObject(){
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
function showdata()
{
var xmlhttp=makeRequestObject();
var user=document.getElementById('user_name').value;
var pass=document.getElementById('password').value;
var url = 'https://121.0.0.1/login'; // invalid ip address for localhost
var params = 'uname='+user+'&'+'pass='+pass;
xmlhttp.open('POST', url, true);
xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status == 200) {
var content = xmlhttp.responseText;
if( content ){
document.getElementById('userdiv').innerHTML = content;
}
}
}
xmlhttp.send(params);
}
</script>
<body>
Enter user name : <input type="text" id="user_name" /><br/>
Enter your password : <input type="text" id="password"/><br/>
<input type="button" onclick="showdata()" value="Submit"><br/><br/>
<div id="userdiv">
</div>
</body>
</html>
I have the following thin handler function on my server side:
$app->post('/login', function () use ($app){
$req = $app->request();
$uname = $req->params('uname');
$pass = $req->params('pass');
$app->response()->header('Content-Type', 'application/json');
echo json_encode($uname);
exit;
});
My problem is that the ajax call is being made on the server as the request is being displayed on the server. But no response is returned from the server. Please, help.
here are screenshots of what is happening in chrome develoepr tool
http://s29.postimg.org/5379a7rt3/image.png
I can see that the initial call to localhost works but the next ajax call to login does not work as only the request is created but no response is received. The response subsequently expires
source to share