Implement Web Services in PHP & JavaScript. -
i want use web services ajax php , javacript, thied exemple still have error. tried lot of code, please me.
xmlhttprequest cannot load xxxxx/login.php. no 'access-control-allow-origin' header present on requested resource. origin 'null' 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 , $_post['username'] != "" , $_post['password'] != null , $_post['password'] != ""){ $username = $_post['username']; $password = $_post['password']; } if($username == "youssef" , $password=="4656" ){ echo "nice"; } else { echo "nono";} } ?>
if url requesting not on same domain request originates browser security prevent request completing. due same origin policy.
to work around need make jsonp request or use cors, assuming third party supports them, or make server-side proxy make request , make ajax request local url.
Comments
Post a Comment