[英]XMLhttprequest gets '404 not found' but invoke-webrequest runs fine
I'm trying to run a Citrix application from javascript code. 我正在尝试从javascript代码运行Citrix应用程序。 I've got a sample that works fine with powershell, just showing first call: 我有一个适用于PowerShell的样本,只显示第一个调用:
#Gets required tokens
$headers = @{
"Accept"='application/xml, text/xml, */*; q=0.01';
"Content-Length"="0";
"X-Requested-With"="XMLHttpRequest";
"X-Citrix-IsUsingHTTPS"="Yes";
"Referer"=$sfurl;
}
Invoke-WebRequest -Uri ($sfurl + "Home/Configuration") -Method POST -Headers $headers -WebSession $sfsession|Out-Null
$csrf = $sfsession.cookies.GetCookies($sfurl)|where{$_.name -like "CsrfToken"}
$cookiedomain = $csrf.Domain
write-host $csrf.Domain
that piece of code works fine, and we can see the value provided from citrix server but if I translate it to javascript I only get error '404 Not Found'. 这段代码工作正常,我们可以看到citrix服务器提供的值,但如果我将其翻译为javascript我只会收到错误'404 Not Found'。 Javascript piece of code is also simple: Javascript片段也很简单:
function starticaurl (authurl, whatapp) {
var csrf = getCookie("CsrfToken");
var xhr = new XMLHttpRequest();
xhr.open("POST", (authurl + "Home/Configuration"), true);
xhr.setRequestHeader("Accept", "application/xml, text/xml, */*; q=0.01");
//xhr.setRequestHeader("Content-Length","0");
xhr.setRequestHeader("X-Citrix-IsUsingHTTPS","Yes");
//xhr.setRequestHeader("Referer",authurl);
xhr.setRequestHeader('Csrf-Token',csrf);
xhr.setRequestHeader("Access-Control-Allow-Origin","*");
xhr.onreadystatechange = function () {
// do something to response
console.log(this.responseText);
};
xhr.send();
console.log("Got: " + csrf);
csrf is always 'null'. csrf始终为“null”。 This function is called from a simple html: 这个函数是从一个简单的html调用的:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>
</head>
<body>
<h1>ICA Launcher</h1>
<script src="createica.js"></script>
<button onclick="starticaurl('https://citrix.mycompany.com/Citrix/TestWeb/', 'Notepad')">Launch App</button>
</body>
</html>
I don't understand why browser is always returning this 404 error when from powerhell same URL is working fine. 我不明白为什么浏览器总是在从powerhell相同的URL工作正常时返回此404错误。
Any idea ? 任何想法 ?
As pointed before behavior is because the browser. 如前所述,行为是因为浏览器。 It requires valid CORS headers from server so it is needed to start Chrome with --disable-web-security to bypass and test. 它需要来自服务器的有效CORS头,因此需要使用--disable-web-security启动Chrome以绕过并测试。 Also to point I've wasted time because the server error is '404 Not found' instead of forbidden or something else... 另外指出我浪费时间,因为服务器错误是'404 Not found'而不是禁止或其他...
I understand also that powershell cmdlet invoke-webrequest is not subject to CORS restrictions at all. 我也理解powershell cmdlet invoke-webrequest根本不受CORS限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.