分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

XMLHttpRequest Basic 认证

发布时间:2023-09-06 01:45责任编辑:胡小海关键词:暂无标签
  • 前端(JS):
    a. 要点:

    b. 完整代码:
    <script>if(window.XMLHttpRequest){ ?// code for IE7+, Firefox, Chrome, Opera, Safari ???xmlhttp=new XMLHttpRequest();}else{ ?// code for IE6, IE5 ???xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}var async = trueif(async === true){ ???xmlhttp.onreadystatechange = function(){ ???????if(xmlhttp.readyState < 4){ ???????????console.log(‘loading...‘) ???????}else if (xmlhttp.readyState == 4 && xmlhttp.status == 200){ ???????????xmlDoc=xmlhttp.responseText; ???????????console.log(xmlDoc) ???????}else{ ???????????console.log(‘fail...‘) ???????????xmlhttp.abort() ???????} ???}}xmlhttp.open("POST",‘http://192.168.0.200/index.php‘,async);xmlhttp.withCredentials = true;xmlhttp.setRequestHeader("Authorization", "Basic " + btoa("user:pass"));xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");xmlhttp.send(‘name=lee‘);if(async === false){ ???xmlDoc=xmlhttp.responseText; ???console.log(xmlDoc)}</script>
  • 后端(PHP):
    <?phpif(!(isset($_SERVER[‘PHP_AUTH_USER‘],$_SERVER[‘PHP_AUTH_PW‘])&&$_SERVER[‘PHP_AUTH_USER‘]==‘user‘&&$_SERVER[‘PHP_AUTH_PW‘]==‘pass‘)){header(‘WWW-Authenticate:Basic realm ="Restricted area"‘);}else{ ?// 验证成功echo "Success";}
  • XMLHttpRequest Basic 认证

    原文地址:http://blog.51cto.com/12173069/2085960

    知识推荐

    我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
    icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved