【问题标题】:Cross Domain AJAX POST / HTTPS / Header Authentication?跨域 AJAX POST / HTTPS / 标头身份验证?
【发布时间】:2011-04-01 20:43:59
【问题描述】:

我有问题:

1) 我尝试过使用 JsonP,但无法让 POST 工作。本质上,我正在尝试使用 API 进行身份验证,通过 HTTPS 在标头中传递 Base64 编码的名称值对。

2) 如何在标头中传递此键/值?任何帮助,将不胜感激!这是我想要的示例,尽管这显然行不通:

               // where does this go?
               var headerString = 'user=' + encodeURIComponent(username + ':' + password);
               $.ajax({
                    type: "POST",
                    url: "https://anotherurl.on.another.server/LOGIN",
                    data: "I have no data, I'm logging in with header authentication",
                    dataType: "json",
                    success: function(data) {

                    },
                    error: function(data){

                  }
                });

【问题讨论】:

  • jsonp 使用 GET 请求......它有点像黑客
  • 我想知道为什么我会收到有趣的 console.log 错误。对“回调”有意义。

标签: javascript ajax jquery https


【解决方案1】:

将标头添加到 ajax 调用:

var headerObj = {'user': encodeURIComponent(username + ':' + password)};
$.ajax({
     type: "GET",
     url: "https://anotherurl.on.another.server/LOGIN",
     data: "I have no data, I'm logging in with header authentication",
     dataType: "json",
     headers: headerObj,
     success: function(data) {

     },
     error: function(data){

   }
});

【讨论】:

  • API中的路由除了GET,只有POST。
  • 然后把type: "GET"改成type: "POST"
【解决方案2】:

执行此操作的最佳方法可能是通过您自己域上的服务器端代理。 有关提示,请参阅this page

这样你就可以得到其他服务器的响应

【讨论】:

  • 我不想混合 PHP;我已经可以做到了:)
猜你喜欢
  • 2017-05-08
  • 2016-05-04
  • 2018-01-26
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
相关资源
最近更新 更多