【问题标题】:How to pass the authorization in ajax CORS request in Firebug?如何在 Firebug 中通过 ajax CORS 请求中的授权?
【发布时间】:2013-04-09 05:56:07
【问题描述】:

我正在尝试通过 ajax jquery 向托管在 https 上的服务器发出管理请求。不知何故,标头身份验证参数未在请求中传递,我收到 401 错误。 我无法传递标头授权信息,并且收到 401 未经授权的错误。如何在标头中传递授权? 任何机构以前都遇到过这个问题,任何帮助将不胜感激。谢谢。

下面是我的ajax请求:::

$.ajax({
    beforeSend: function(xhrObj){
         xhrObj.setRequestHeader("Authorization","Basic YWRtaW46YWRtaW4=");
         xhrObj.setRequestHeader("Access-Control-Allow-Origin","*");
         xhrObj.setRequestHeader("Access-Control-Allow-Methods","POST, GET, OPTIONS, PUT, DELETE");
         xhrObj.setRequestHeader("Access-Control-Allow-Headers","Content-Type");
         } ,
    url: '/test.html',
    type : "POST",
    dataType : "json", 
    crossDomain: true,
    xhrFields: {
        withCredentials: true
        },
    contentType: "application/json" , 
    data: {
        'name'    : 'test123456',
        'description'   : 'test' ,
        'owner'   : 'admin' ,
        'accessRestriction'   : 'VIEW' 
        },
    cache : true, 
     success : function(service_data) {
         alert("success");
      },
     error : function(msg) {
         alert("error");
       }
 });

【问题讨论】:

  • Ajax 请求通常仅限于相同的域、端口和协议——https 可能是问题所在。请求也需要来自 https。
  • 好吧,你解决了吗?

标签: jquery ajax cors


【解决方案1】:

如果您在允许标头中提供content-type,则还需要通过授权。

【讨论】:

    猜你喜欢
    • 2015-03-15
    • 2020-01-31
    • 1970-01-01
    • 2015-07-06
    • 2013-04-10
    • 2019-01-15
    • 2011-09-30
    • 2019-09-24
    • 1970-01-01
    相关资源
    最近更新 更多