I want to use web services with ajax php and javacript, thied this exemple but I still have this error. I tried a lot of code, please someone help me.
XMLHttpRequest cannot load xxxxx/login.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. index.html:1
index.html
<html><head>
<script src="jquery-2.1.0.js"></script>
<script src="jsjsjs.js"></script>
</head>
<body>
<div id='logindiv'>
<label>Username:</label>
<input name="username" id="username" type="text">
<label>Password:</label>
<input name="password" id="password" type="password">
<input value="Submit" name="submit" class="submit" type="submit" onclick='chk_ajax_login_with_php();'>
<div id='status'></div>
</div>
</body>
</html>
jsjsjs.js
function chk_ajax_login_with_php(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
var params = "username="+username+"&password="+password;
var url = "xxxxx/login.php";
$.ajax({
type: 'POST',
url: url,
dataType: 'html',
data: params,
beforeSend: function() {
document.getElementById("status").innerHTML= 'checking...' ;},
complete: function() { },
success: function(html) {
document.getElementById("status").innerHTML= html;
if(html=="success"){
window.location ="/test.php"
}
}
});
}
login.php
<?php
if ($_POST['username'] != null and $_POST['username'] != "" and $_POST['password'] != null and $_POST['password'] != ""){
$username = $_POST['username'];
$password = $_POST['password'];
}
if($username == "youssef" and $password=="4656" ){
echo "Nice";
}
else { echo "nono";}
}
?>