通过AJAX和PHP基本HTTP认证 [英] basic http auth via ajax and php

查看:193
本文介绍了通过AJAX和PHP基本HTTP认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使通过AJAX一个HTTP基本身份验证。

I'm trying to make a http basic auth via ajax.

    $.ajax({
        url: 'secure/index.php',
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Authorization", "Basic " + Base64.encode("user" + ":" + "password") //May need to use "Authorization" instead
        },
        success: function(result) {
            alert(result);
        },
        error:function (xhr, ajaxOptions, throwBnError){
            alert(xhr);
            alert(thrownError);
        }  

确定。问题是,我不使用任何.htaccess文件做HTTP认证。

Ok. The problem is that I don't use any .htaccess file to do the HTTP auth.

我有一个简单的PHP文件,其中这使得认证:

I have a simple PHP file where this make the authentication:

header('WWW-Authenticate: Basic realm="Secret page"');
header('HTTP/1.0 401 Unauthorized');

和我的Ajax请求不希望工作..

And my ajax request does not want to work..

我是什么做错了吗?

推荐答案

您是否尝试过使用在阿贾克斯的用户名和密码字段?

Have you tried using the username and password fields in .ajax?

$.ajax({
    url: "http://mydomain/mypage.php",
    username: u,
    password: p,
    error: function () {
    }
}).done(function(html) {
        //Do Stuff
});

这对我的作品,几乎所有的时间。

This works for me almost all of the time.

这篇关于通过AJAX和PHP基本HTTP认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆