[英]Fetch one entity by url location header of Spring POST response in restangular
[英]Using a Location header in Fetch response
我正在使用获取请求,并且我想在响应中检测位置 header 以便我可以根据是否存在位置 header 做出不同的反应,如下所示:
fetch(request)
.then(response => {
for(var keyValPair of response.headers.entries())
{
alert(keyValPair[0]);
if(keyValPair[0] === 'Location')
{
alert('TRYING TO RELOCATE');
window.location.replace(keyValPair[1]);
return;
}
}
if(response.ok)
{
return response.text();
}
else
{
document.getElementById(targetBlock).innerHTML = 'ERROR! ERROR! There has been an ERROR!'
}
})
.then(removeBlock('alertMessageBlock'))
.then(function(text){document.getElementById(targetBlock).innerHTML = text;})
.catch(error => console.log('There was an error:', error))
}
它获取的 php 文件如下:
<?php
require __DIR__ . '/../../private_templates/Template/varsConstants.php';
require_once __DIR__ . '/../../private_templates/Modules/functions.php';
require __DIR__ . '/../../private_templates/Modules/dbConnection.php';
require_once __DIR__ . '/../../private_templates/Modules/DatabaseTable.php';
require_once __DIR__ . '/../../private_templates/Modules/Authentication.php';
require_once __DIR__ . '/../../private_templates/Modules/memberClass.php';
if (!isset($alertMessage))
{
$alertMessage = [];
}
$membersTable = new DatabaseTable($dbc, 'member', 'memberID');
$authentication = new Authentication($dbc, $membersTable, 'email', 'pass');
if ($authentication->isLoggedIn())
{
?>
<form data-targetBlock="adminMembers" class="fetchForm" action="<?=ADDRESS ?>/MAINhubs/privateHub.php" method="post">
<fieldset>
<legend>Search for Members By:</legend>
<label for="searchType">Choose Search Parameter:</label>
<select name = "searchType">
<option value = "none" selected>Choose parameter to search by</option>
<option value = "registrationDate">Registration Date</option>
<option value = "firstName">First Name</option>
<option value = "lastName">Last Name</option>
<option value = "email">Email</option>
<option value = "company">Company</option>
<option value = "city">City</option>
<option value = "region">Region</option>
<option value = "country">Country</option>
</select>
<button type="submit" name="formTarget" value="adminMembersSearch">Load Members Search Form</button>
</fieldset>
</form>
<div id="adminMembers">
</div>
<?php
}
else
{
header('Access-Control-Expose-Headers: Location');
header('Location: ' . ADDRESS);
}
require_once __DIR__ . '/../../private_templates/Sections/alertMessageBlock.php';
?>
提取工作正常,位置 header 工作正常,但我拦截 header 无法正常工作,我希望整个 window 重定向到该位置而不是插入目标。 我觉得我需要公开位置 header,但我不知道该怎么做......有什么帮助吗?
使用位置 header 不起作用,所以我做了一个自定义 header 代替:
else
{
header('goto: ' . ADDRESS);
}
然后我能够在获取响应中看到 header 并在此处按预期使用它。
fetch(request)
.then(response => {
for(var keyVal of response.headers.entries())
{
if(keyVal[0] === 'goto')
{
window.location.href = keyVal[1];
return;
}
}
if(response.ok)
{
return response.text();
}
else
{
document.getElementById(targetBlock).innerHTML = 'ERROR! ERROR! There has been an ERROR!'
}
})
.then(removeBlock('alertMessageBlock'))
.then(function(text){document.getElementById(targetBlock).innerHTML = text;})
.catch(error => console.log('There was an error:', error))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.