javascript - Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers in preflight response -
i trying make login page cross domain couldn't solve problem, error is:
xmlhttprequest cannot load http://localhost/testing/resp.php. request header field access-control-allow-headers not allowed access-control-allow-headers in preflight response.
my javascript code is:
$('#login').click(function(){ var username = $('#uname').val(); var password = $('#pass').val(); var result = $('.result'); result.text('loading....'); if (username != '' && password !=''){ var urltopass = 'action=login&username='+username+'&password='+password; $.ajax({ type: 'post', data: urltopass, headers: {"access-control-allow-headers": "content-type"}, url: 'http://localhost/testing/resp.php', crossdomain: true, cache: false, success: function(responsetext){ console.log(responsetext); if(responsetext== "0"){ result.text('incorrect login information'); } else if (responsetext == "1"){ window.location="http://localhost/testing/home.php"; } else{ alert('error in sql query \n' + responsetext); } } }); } else return false; });
the php code http://localhost/testing/resp.php :
<?php include "db.php"; //connecting database if (!isset($_server['http_origin'])) { echo "this not cross-domain request"; exit; } header("access-control-allow-origin: *"); header("access-control-allow-credentials: true"); header("access-control-allow-methods: post, get, options"); header("access-control-allow-headers: content-type, authorization, x-requested-with"); header('p3p: cp="cao psa our"'); // makes ie support cookies header("content-type: application/json; charset=utf-8"); if (isset($_post['action']) && $_post['action'] == 'login'){ $uname = $_post['username']; $pass = $_post['password']; $sql = "select * loginajax username='$uname' , password='$pass'"; $rs=$conn->query($sql); if (mysqli_num_rows($rs) <= 0){ echo "0"; } else { echo "1"; } } else echo "this not login"; ?>
remove this:
headers: {"access-control-allow-headers": "content-type"},
from jquery.ajax call.
the server responds access-control-allow-headers
header, client doesn't send server.
the client sends access-control-request-headers
request allowing headers, server responds with access-control-allow-headers
lists actual headers going allow. client not demand headers allowed.
Comments
Post a Comment