javascript - jQuery Ajax 登录中没有 'Access-Control-Allow-Origin' header

标签 javascript php jquery ajax authentication

我有一个名为 login.php 的登录页面,它位于主第一根级别,而 users.php 位于 assets/app/users 中。 php.我正在使用 jQuery ajax 来完成登录过程。在 login.php 的 JS 部分我有:

    if(loginProceed){
        var data = {email: loginEmail, pass:  loginPass};
        var loginreq = $.ajax({
                                type: "POST",
                                url : "assets/app/users.php",
                                cache: false,
                                data: data
                          });

    loginreq.done(function(html) {
   if(html=='true'){
       window.location.replace('app.php'); }
   else  {
    $("#loginRequest").before('<div class="alert alert-danger err" role="alert">Email or Password Is Not Correct</div>');
    }
    });    
}
     e.preventDefault();
});

users.php中我有

<?php
header('Access-Control-Allow-Origin: *');
session_start(); 
include 'config.php';
           $email = $_POST['email'];
           $pass  = $_POST['pass'];
           $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);                
           $sql = "SELECT id, email, fname, lname, type FROM users WHERE `email`=? AND `pass`=?";              
           $stmt = $conn->prepare($sql);
           $stmt->bind_param('ss', $email,$pass);
           $stmt->execute();
           $stmt->bind_result($theId,$theEmail,$theFName,$theLname,$theType);
           if ($stmt->fetch()) {
                echo 'true';
                $_SESSION['LOGIN_STATUS'] = true;
                $_SESSION['fname'] = $theFName;
           } else {
                echo 'false';
            }

现在,这正在所有浏览器中为我执行登录过程,但对于其他一些用户,它仅适用于 Chrome!或者甚至没有一个,我们在控制台中收到这些错误:

login.js:74 Object login.php:1 XMLHttpRequest cannot load http://domain.ca/login.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.domain.ca' is therefore not allowed access.

或在 FireFox 上

Password fields present on an insecure (http://) page. This is a security risk that allows user login credentials to be stolen.[Learn More] "SensorNotRequired: https://developers.google.com/maps/documentation/javascript/error-messages" util.js:30:57

以及 EI

SCRIPT7002: XMLHttpRequest: Network Error 0x4c7, The operation was canceled by the user.

您能告诉我为什么会发生这种情况吗?

最佳答案

尝试允许单个域

header('Access-Control-Allow-Origin: http://www.domain.ca');

或尝试使用 Header set Access-Control-Allow-Origin "*"修改 .htaccess enter link description here

看起来你的脚本是从domain.ca加载的(没有www),但是你或其他用户从www.domain.ca向php发出请求

关于javascript - jQuery Ajax 登录中没有 'Access-Control-Allow-Origin' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33554719/

相关文章:

javascript - 复杂的Javascript计算

javascript - Firebase 将集合作为数组返回

php - PHP 中 Set X = Nothing 的等价物是什么?

Java & PHP 对称加密 (DES)

javascript - 刷新页面时Angular js给出404错误

javascript - jQuery - mouseenter/mouseleave 定时器不起作用

PHP LDAP 无法安全工作

jquery - 聊天框中的 CSS 问题,如 Chrome 中包含 iframe 的 div

javascript - 如何从下拉菜单选择中显示图像?

jquery - 检查元素时仅在 bx slider 中加载图像,在 Bootstrap 模式中添加 bx slider ,这是 css 问题吗?