servicestack,cors,Jquery,servicestack,Cors" /> servicestack,cors,Jquery,servicestack,Cors" />

ajax jquery跨域调用不发送授权标头

ajax jquery跨域调用不发送授权标头,jquery,servicestack,cors,Jquery,servicestack,Cors,在使用服务堆栈对服务器进行跨域调用之前,我已成功通过身份验证并获取令牌 现在我要执行另一个调用来检索数据: $.ajax({ beforeSend: function(xhr) { xhr.setRequestHeader('Authorization', 'Basic ' + getToken()); }, type: "GET", url: requestUrl, xhrFields: {

在使用服务堆栈对服务器进行跨域调用之前,我已成功通过身份验证并获取令牌

现在我要执行另一个调用来检索数据:

$.ajax({
    beforeSend: function(xhr) {                  
        xhr.setRequestHeader('Authorization', 'Basic ' + getToken());   
    },
    type: "GET",
    url: requestUrl,
    xhrFields: {
        withCredentials: true
    },  
    async: true,
    dataType: 'json',
    crossDomain: true
})
当我在google chrome开发工具控制台中查看时,我看到:

OPTIONS http://MyPc.company:82//customers 404 (Not Found) 
OPTIONS http://MyPc.company:82//customers Invalid HTTP status code 404 

XMLHttpRequest cannot load http://MyPc.company:82//customers. 
Invalid HTTP status code 404 (index):1
当我观察fiddler时,我看到了这个请求:

Inspectors=>Auth:不存在授权标头

检查员=>原始:

OPTIONS http://MyPc.company:82//customers HTTP/1.1
Host: MyPc.company:82
Connection: keep-alive
Cache-Control: no-cache
Pragma: no-cache
Access-Control-Request-Method: GET
Origin: http://MyPc.company
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Access-Control-Request-Headers: access-control-allow-origin, accept, access-control-allow-headers, authorization, access-control-allow-methods, content-type
Accept: */*
Referer: http://MyPc.company/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
为什么未发送授权标头?乍一看,这似乎是我的起源问题

     jQuery.support.cors = true;

   function make_base_auth(user, password) {
      var tok = user + ':' + password;
      var hash = btoa(tok);
      return "Basic " + hash;
  }
   function DoTest() {
          var TestRequest = new Object();
          TestRequest.name = "Harry Potter";             
          TestRequest.Id = 33;
         var username = "admin";
         var password = "test"; 
      $.ajax({
          type: 'Post',
          contentType: 'application/json',
          cache: false,
          async: false,
          url: serverIP + '/TestAPI/'+ TestRequest.Id,
          data: JSON.stringify(TestRequest),
          dataType: "json",                  
          beforeSend: function (xhr) {                    
           xhr.setRequestHeader("Authorization", make_base_auth(username, password));
          },
       success: function (response, status, xhr) {
              var s= response.message;      
          },
          error: function (xhr, err) {
              alert(xhr.statusText);
          }
      });
  }
应为COR(如)启用服务配置

              Plugins.Add(new CorsFeature(allowedHeaders: "Content-Type, Authorization")); 
也许我的前任能帮你

或者更好的是,下面的博文来自


您无法控制随飞行前(选项)请求发送的标题。阅读这里的预飞行:@raynocholus“…因为设置了自定义标头,所以该请求是预飞行的。”所以现在我知道我做预飞行请求是因为我使用了authorizationheader?但是,还有什么方法可以将令牌发送回令牌来检查它呢?你能读懂吗,也许它解决了你的问题。你解决过这个问题吗?我将你的JS代码与我的JS代码进行了比较,所有重要的事情似乎都是一样的,只是你没有设置crossDomain=true。当我查看您的Servicestack配置时,您是对的,我不记得CorsFeature具有allowedHeaders设置的授权,但我怀疑这是我的问题。我的问题是根本没有发送头,这意味着我可以在服务器端配置我想要的,对吗?但我还是会根据您的提示检查我的服务配置;所以我猜这和我设置的crossDomain=true相同?我认为这是服务配置。如果从服务中注释掉属性[Authenticate],它是否正常工作?关于jQuery.support.cors=true;是的。您确定您的服务是为CORS配置的吗?来自fiddler的请求只是选项。我现在已经在服务器端尝试了:Plugins.Add(新的CorsFeature(allowedOrigins:Settings.Default.SmartAllowedCorsOrigin,allowCredentials:true,allowedHeaders:“Authorization”);但是authorize标头没有像我前面提到的那样发送,这是最初的问题。