I am trying to get response from my server which is running php file login.php. I am running t3.html on computer and login.php on my server
t3.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PHP JSON Testing</title>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
</head>
<body>
<div id="form">
<table>
<tr>
<td>Field 1</td>
<td><input type="text" id="text1" /></td>
</tr>
<tr>
<td>Field 2</td>
<td><input type="text" id="text2" /></td>
</tr>
<tr>
<td colspan="2"><button id="submit">Send Request</button> </td>
</tr>
</table>
</div>
<div>
Response:
<span id="response"></span>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#submit").on("click", function(event){
var text1 = $("#text1").val();
var text2 = $("#text2").val();
var jsonData = {
field1 : text1,
field2 : text2
};
$.ajax({
type: "POST",
url: "http://X.X.X.X/test/login.php",
data: JSON.stringify(jsonData),
contentType: "application/json",
dataType: "json",
success: function(response) {
$("#response").html(JSON.stringify(response));
},
failure: function(error){
$("#response").html(error);
}
});
$("#text1").val("");
$("#text2").val("");
});
});
</script>
</body>
</html>
login.php
<?php
$json = file_get_contents("php://input");
$obj = json_decode($json);
$response = array();
$response["data1"] = $obj->field1;
$response["data2"] = $obj->field2;
$json_response = json_encode($response);
echo $json_response;``
header("Content-type: application/json");
?>
But it is not getting any response, but when i kept my html file on the server it worked please help me so that i can run the html file on my computer and get a response from server login.php