1
当前我有一种提交表单POST并在新窗口中打开的JavaScript方法。我想在AngularJS中重写这个。使用AngularJS发布到新窗口
这里是现有的方法。传入的3个参数仅影响POST后的URL和一些数据值。
var login = function(postUrl, samlResponse, samlDomain){
var form = $('<form></form>');
form.attr('method', 'post');
form.attr('action', postUrl);
form.attr('target', '_blank');
var field = $('<input></input>');
field.attr('type', 'hidden');
field.attr('name', samlResponse);
field.attr('text-wrap','none');
field.attr('value', 'response-value');
form.append(field);
var field2 = $('<input></input>');
field2.attr('type', 'hidden');
field2.attr('name', 'RelayState');
field2.attr('value', samlDomain);
form.append(field2);
$(document.body).append(form);
form.submit();
}
这是我尝试使用$ HTTP
$scope.login = function(postUrl, samlResponse, samlDomain) {
var tabWindowId = window.open('about:blank', '_blank');
var data = {
SAMLResponse: samlResponse,
RelayState: samlDomain
}
$http.post(postUrl, data).then(function (response) {
tabWindowId.location.href = response.headers('Location');
});
}
我还发现了以下错误,发生因为网站不会让我访问响应为此在AngularJS。我不一定需要访问响应,我只是想在新窗口中打开它。
XMLHttpRequest cannot load {{postUrl}}. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:7631' is therefore not allowed access.
我应该如何完成我想要的AngularJS?
我也在考虑将表单添加到HTML模板,但隐藏它与ng隐藏。然后,我不得不以某种方式触发从我的AngularJS控制器中提交的表单,这看起来倒退了。