简体   繁体   English

XMLhttprequest获取'404 not found'但是invoke-webrequest运行正常

[英]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.

相关问题 XMLHttpRequest检测到404(未找到) - XMLHttpRequest detecting 404 (Not Found) 在前端使用 XMLHttpRequest 发送的 PUT、DELETE 会给出 404 响应,但在 postman 中工作得很好 - PUT, DELETE sent using XMLHttpRequest on frontend give 404 response, but in postman work just fine Javascript WebRequest调用/缓存问题 - Javascript WebRequest Invoke/Cache problem Angular $ resource方法PUT获取在服务器上找不到404 - Angular $resource method PUT gets 404 not found on server JS 文件得到一个 net::ERR_ABORTED 404 (Not Found) - JS file gets a net::ERR_ABORTED 404 (Not Found) xmlhttprequest仅进入状态3 - xmlhttprequest onlys gets to status 3 XMLHttpRequest状态返回404 - XMLHttpRequest status returning 404 XMLHttpRequest失败:{statusText“:未找到”,“ status :: 404”,“ responseURL”:https://api.parse.com/1/users” Ionic + Parse - XMLHttpRequest failed: {statusText“:Not Found”,“status::404,”responseURL“:https://api.parse.com/1/users” Ionic + Parse (当我有 JavaScript 运行 PHP 脚本时未找到 PHP 文件路径,但出现 ZDA88B4E490C14359B639CZ4A 错误); 该怎么办? - PHP file path not found when I have JavaScript run a PHP script with XMLHTTPREQUEST (404 error); what to do? .net web 应用程序在 VS 中运行,但在 IIS 中获得 404,生成错误的 ZE6B391A8D2C4D45508 - .net web app runs in VS, but gets 404 in IIS, generates wrong URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM