gpt4 book ai didi

javascript - 如何在asp.net MVC中使用href从javascript在新窗口中打开打开的窗口

转载 作者:行者123 更新时间:2023-12-03 03:25:33 24 4
gpt4 key购买 nike

我有以下代码,我从我的 asp.net MVC View 中打开外部 url 链接。

现在我必须在打开链接时添加一个条目。我的想法是,我应该通过一些 javascript 函数来完成此操作,该函数将调用 ajax 调用我的 web api Controller ,然后打开一个新的 url 窗口

 @{
var token = Session["SecurityToken"] as AppSecurityToken;
if (token != null && token.Claims != null)
{
foreach (var claim in token.Claims)
{
if (!String.IsNullOrWhiteSpace(claim.Description))
{
string imagepathv = Url.Content("~/") + "images/" + claim.Name + ".png";
string hreftext = $"{claim.Description}?sessionid={token.SessionId}";
<li>
<a href="" onclick="UserNameAccess('@hreftext');">
<img src="@imagepathv" />
</a>
</li>
}
}
}
}


<script lang="javascript" type="text/javascript">
function UserNameAccess(input) {
window.open(input, "_blank");


var baseSiteUrl = '@ConfigurationManager.AppSettings["WebApiBaseUrl"]';
var request = { "email": "sadasd", "firstname": "asdasd", "lastname": "dasdasda" };

$.ajax({
type: "POST",
url: baseSiteUrl + "DataAccessApi/LogUserClaimAccesshistory",
data: JSON.stringify(request),
datatype: "text",
contentType: "application/json",
success: function (resultData) {
var responseJson = resultData;
if (responseJson.Success === true) {
alert("success");
}
else {
alert("error1");
}
},
failure: function (error) {
alert(error);
},
error: function (jqXhr, textStatus) {

if (textStatus === 'timeout') {
alert("Request Timeout");
//do something. Try again perhaps?
} else {
alert(jqXhr.statusText);
}
},
timeout: 30000 // sets timeout to 3 seconds
});
}
</script>


public class DataAccessController : ApiController
{

[Route("DataAccessApi/LogUserClaimAccesshistory")]
public MyJsonResponse LogUserClaimAccesshistory(UserClaimHistory request)
{
MyJsonResponse result = new MyJsonResponse();
return result;
}
}


public class MyJsonResponse
{
[JsonProperty]
public bool Success { get; set; }

[JsonProperty]
public string Message { get; set; }

[JsonProperty]
public string CustomInfo { get; set; }
}


public class UserClaimHistory
{
[JsonProperty("email")]
public string Email { get; set; }

[JsonProperty("firstname")]
public string FirstName { get; set; }

[JsonProperty("lastname")]
public string LastName { get; set; }
}

最佳答案

为什么不使用下面的代码?让 a 标签来打开网页,而 JS 负责 API 调用。

更新的答案:

@{
var token = Session["SecurityToken"] as AppSecurityToken;
if(token != null && token.Claims != null)
{
foreach(var claim in token.Claims)
{
if(!String.IsNullOrWhiteSpace(claim.Description))
{
string imagepathv = Url.Content("~/") + "images/" + claim.Name + ".png";
string hreftext = $"{claim.Description}?sessionid={token.SessionId}";
<li>
<!-- We use the <a> tag to handle the new page opening -->
<a href="@hreftext" onclick="UserNameAccess();" target="_blank">
<img src="@imagepathv"/>
</a>
</li>
}
}
}
}

<!-- The javascript completly ignores the new page, and only focus on the API call -->
<script lang="javascript" type="text/javascript">
function UserNameAccess()
{
var baseSiteUrl = '@ConfigurationManager.AppSettings["WebApiBaseUrl"]';
var request = { "email": "sadasd", "firstname": "asdasd", "lastname": "dasdasda" };

$.ajax({
type: "POST",
url: baseSiteUrl + "DataAccessApi/LogUserClaimAccesshistory",
data: JSON.stringify(request),
datatype: "text",
contentType: "application/json",
success: function(resultData)
{
var responseJson = resultData;
if (responseJson.Success === true)
{
alert("success");
} else
{
alert("error1");
}
},
failure: function(error)
{
alert(error);
},
error: function(jqXhr, textStatus)
{
if (textStatus === 'timeout')
{
alert("Request Timeout");
//do something. Try again perhaps?
} else
{
alert(jqXhr.statusText);
}
},
timeout: 30000 // sets timeout to 3 seconds
});
}
</script>

关于javascript - 如何在asp.net MVC中使用href从javascript在新窗口中打开打开的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46348219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com