首页 > 解决方案 > 如何在单击 href 时重定向到 URL?

问题描述

我的问题首先是尝试在单击 href 时重定向到 URL,并且我正在通过 POST 请求中的 URL 传递请求有效负载,请求有效负载包含用户名和密码,并且基本上尝试在链接上进行自动身份验证。请就这种方法是否正确以及如何实现这一点提出建议。谢谢!

我已经尝试使用 create Element 函数创建一个 href 并在 fetch api 中传递带有有效负载的 URL。

export default class PortComponent extends Component {
constructor(props) {
        super(props);
        this.state = { 
                      columnDefs: [
                {headerName: "Details", field: "details", autoHeight: true,
                cellRenderer: (params) => {

                      var link;

                      link = document.createElement('a');
                      link.href = "#";
                      link.onclick=this.manageMailWeb();
                      link.innerText = "Manage Mail + Web";
                      link.target="_blank";
                      div.appendChild(link);
                   }
                   return div;
                };
        }



                      async manageMailWeb(){
                      let name,pass, url = "https://example.net/OS4/index.php";
                      this.state.items.map(e => {
                              name=e.serviceDetails.domainName;
                              pass=e.serviceDetails.domainPassword;
                      });
                     await redirectService(name,pass,url);
                  }
        static async redirectService(name,pass,url,serviceId){
        const user=name,
        url = "",
        appoverride = "Login",
        CustomLogin = "http://example.net/login/close.html",
        CustomError = "http://example.net/login/close.html",
        CustomShell = "http://example.net/login/close.html",
        CustomLanguage = 'en',
        login_x = "1",
        login_y="1";
        return await fetch(url, {
         mode: "no-cors",
         method: "POST" ,
         headers: {
           "Access-Control-Allow-Origin": "*",
           "Content-Type": "application/json"
         },
         body: JSON.stringify(user,pass,appoverride, 
   CustomLogin,CustomError,CustomShell,CustomLanguage,login_x,login_y),})
   }

标签: javascripthtmlreactjsfetchhref

解决方案


您应该像这样调用没有 this 关键字的 manageMailWeb 函数manageMailWeb


推荐阅读