简体   繁体   English

有什么方法可以使 Python 处理即使通过 Ajax 按钮是“打开新标签”也可以执行

[英]Is there any way to make it so that Python processing is performed even if the button is "Open New Tab" via Ajax

I am trying to prevent the user from moving to the manager class target link by using Ajax, but I am facing a problem that if I right-click to open a new tab or open in a new window, the user can move to the target link.我试图通过使用 Ajax 阻止用户移动到经理 class 目标链接,但我面临一个问题,如果我右键单击以打开新选项卡或在新的 window 中打开,用户可以移动到关联。

By the way, the JS is written as follows, and when the button is pressed, Ajax is activated.顺便把JS写成如下,当按下按钮时,Ajax被激活。

$('#button').click(function () {
    const url = '//login?id=', userId);

$.ajax({
  url: url,
  contentType: 'application/x-www-form-urlencoded',
  type: 'GET',
  async: false,
})
  .then(function (data) {
    const nextUrl = data.url;

    if (transferUrl) {
      location.href = nextUrl;
    } else {
      alert('No Authorization');
    }
  })
  .catch(function () {
    alert('Failed');
  });

The button HTML is written as follows.按钮 HTML 编写如下。

<a id="button" href="/admin/top.html">
   <img id="toplnk" src="/images/login/a.png" onmouseover="this.src='/images/login/b.png'" onmouseout="this.src='/images/login/c.png'" alt="page">
</a>

The Python code that is sent and processed by Ajax is as follows. Ajax发送并处理的Python代码如下。

def check_user(self, request):
  user_id = request.GET.get(key='id')
  user = User.objects.all().filter(Q(UserId__exact=user_id), condition_role)
  if user.count() == 0:
     url = ""
  else:
     url = '/admin/top.html'
  json_data = {'url': url}
  return Response(json_data)

Is there any way to make it so that Python processing is performed even if the button is "Open New Tab" via Ajax as shown above?有什么办法可以使 Python 处理即使通过 Ajax 按钮是“打开新标签”,如上所示?

There can be a couple of solutions, you need to decide which one would suit your usecase.可能有几种解决方案,您需要决定哪一种适合您的用例。

  1. You can remove anchor tag href and programmatically navigate to the link using window.location(), this will block the "open in new tab" option.您可以删除锚标记 href 并使用 window.location() 以编程方式导航到链接,这将阻止“在新选项卡中打开”选项。 If you do not require it, you can go with this solution.如果您不需要它,您可以使用此解决方案 go。
  2. If you require open in new tab to be there as an option, then I believe you need to run your ajax on the load of the page you are navigating to, alongwith conditions if you do not want to run this every time on that page.如果您需要在新选项卡中打开作为选项,那么我相信您需要在您要导航到的页面的负载上运行 ajax,以及如果您不想每次都在该页面上运行的条件。 This is typically a usecase similar to running a function on ComponentMount or useEffect in React JS.这通常是一个类似于在 React JS 中的 ComponentMount 或 useEffect 上运行 function 的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过Ajax下载PDF并在新的浏览器选项卡中打开 - Download a PDF via Ajax and Open in a New Browser Tab 单击按钮打开新标签页后继续 JavaScript - Continue JavaScript after open new Tab via Button Click 通过Tampermonkey打开新标签中的点击链接的方法? - Way to Open Clicked Link in New Tab via Tampermonkey? 如何使按钮在JavaScript中的新选项卡中打开链接? - How to make button open a link in new tab in JavaScript? 如何打开没有 URL 的新浏览器选项卡(通过网页上的 onClick),就像您单击“+”按钮添加新选项卡一样 - How to open a new browser tab with no URL (via onClick on webpage) as if you clicked the “+” button to add a new tab 如何在新标签页中打开ajax? - How to open an ajax in new tab? 在新标签页中打开链接的按钮 - Button to open link in new tab 单击带有滚轮的链接会在新选项卡中打开,但在执行右键单击+打开新选项卡时不会打开 - clicking on a link with scroll wheel opens in a new tab but not when right-click + open new tab is performed 即使用户单击“中间按钮”或右键单击-&gt;在新选项卡中打开,如何触发jquery“单击”事件 - How to trigger jquery “click” event even when the user clicked “middle button” or right click -> open in a new tab 通过JS在新标签页中有条件地打开网址 - Conditionally open url in new tab via js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM