【问题标题】:Salesforce Refused to set unsafe header "User-Agent"Salesforce 拒绝设置不安全的标题“用户代理”
【发布时间】:2015-03-23 01:36:05
【问题描述】:

我正在尝试在我的远程应用程序上创建一个用户登录/注册,以访问 Salesforce 上的用户数据。我通过 Salesforce REST API 从 javascript 执行此操作。由于 CORS 限制,这无法直接使用来自 javascript 的请求,所以我找到了this example。我的代码如下:

    var result = sforce.connection.login('example@provider.com', 'pass'+'securityToken');
    sforce.connection.init(result.sessionId, 'https://login.salesforce.com/services/oauth2/token')

    sforce.connection.remoteFunction({
        url: 'https://login.salesforce.com/services/oauth2/token',
        requestHeaders: {
            "Authorization" : "Bearer " +__sfdcSessionId,
            "Content-Type":"application/json",
            "Connection":"Keep-Alive"
        },
        method: "GET",
        onSuccess : function(response) {
            console.log("Success " + response)
        },
        onFailure: function(response) {
            console.log("Failed " + response)
        }
    });

当我运行此代码时,我收到以下错误:

1) 拒绝设置不安全的标头“User-Agent”
2) POST http:///services/Soap/u/31.0 404(未找到)
3) 远程调用失败,原因:

<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /services/Soap/u/31.0 was not found on this server.</p>
</body></html>
 status code: 

使用代码编辑器,我可以看到错误是在点击 sforce.connection.login() 脚本之后发生的,并且从未在我的代码中进入 sforce.connection.init() 脚本。

如何解决此问题,以便我可以从远程 Web 应用程序登录用户并访问 salesforce 中的用户信息?

【问题讨论】:

    标签: javascript ajax salesforce


    【解决方案1】:

    看来您的问题类似于this post中的问题

    XMLHttpRequest 不允许设置这些标头,它们是由浏览器自动设置的。原因是通过操纵这些标头,您可能能够欺骗服务器通过同一连接接受第二个请求,该连接不会通过通常的安全检查 - 这将是浏览器中的一个安全漏洞。

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 2011-12-03
      • 1970-01-01
      • 2016-01-13
      • 2014-07-07
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      相关资源
      最近更新 更多