Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery ajax基本身份验证节点表达式_Jquery_Ajax_Node.js_Express_Basic Authentication - Fatal编程技术网

jquery ajax基本身份验证节点表达式

jquery ajax基本身份验证节点表达式,jquery,ajax,node.js,express,basic-authentication,Jquery,Ajax,Node.js,Express,Basic Authentication,我希望ajax通过基本身份验证,将返回401状态 客户 使用咖啡脚本 使用jquery.base64.js $.ajax url : "http://example.com/" type: "GET" dataType: "text" crossDomain: true beforeSend: (xhr)-> credentials = $.base64.encode("user:pass") xhr.withCredentials = true

我希望ajax通过基本身份验证,将返回401状态

客户 使用咖啡脚本

使用jquery.base64.js

$.ajax
  url : "http://example.com/"
  type: "GET"
  dataType: "text"
  crossDomain: true
  beforeSend: (xhr)->
    credentials = $.base64.encode("user:pass")
    xhr.withCredentials = true
    xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
    xhr.setRequestHeader("Authorization", "Basic " + credentials)

.always (result, textStatus, jqXHR) =>
  if textStatus == "success"
    console.log "success"
服务器 使用咖啡脚本

使用node.js express

router.use (req, res, next)->
  res.header "Access-Control-Allow-Origin", "http://client.com/"
  res.header "Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, HEAD, OPTIONS"
  res.header "Access-Allow-Control-Credentials", "true"
  res.header "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization"
  next()

router.use basicAuth('user', 'pass')

router.get "/", (req, res)=>
  res.json 200, {
    status_code: 0
  }
以下是我尝试实际发送的结果

要求 响应 在本地环境上运行 为什么返回401状态

Remote Address:example.com
Request URL:http://example.com/
Request Method:OPTIONS
Status Code:401 Unauthorized
Request Headers
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Access-Control-Request-Headers:accept, authorization
Access-Control-Request-Method:GET
Connection:keep-alive
Host:example.com
Origin:http://client.com
Referer:http://client.com/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2219.0 Safari/537.36
Response Headers
Access-Allow-Control-Credentials:true
Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept, Authorization
Access-Control-Allow-Methods:GET, PUT, POST, DELETE, HEAD, OPTIONS
Access-Control-Allow-Origin:http://client.com
Connection:keep-alive
Date:Mon, 17 Nov 2014 21:30:08 GMT
Transfer-Encoding:chunked
WWW-Authenticate:Basic realm="Authorization Required"
X-Powered-By:Express