javascript - 如何从 Node js app.post() 调用 html 中的 javascript 函数?

标签 javascript jquery html node.js

我必须从nodejs app.post() 调用存在于html 文件中的javascript 函数。一旦请求到达 app.post(/process-view-dnd-registration),我就必须调用 openup()。这里的 html 文件包含要显示的弹出窗口的设计。我尝试使用 popup.openup() 调用该函数。但这没有用。请在下面找到我的代码。

服务器.js

app.post('/process-view-dnd-registration',function(request,reply){
    popup.openup();
    var json_blob = ({
    "header" : {
    "event_const" : "CNST_PREFERENCE_REGISTRATION_EVENT"
    },
    "jsonblob" : {    
    "MobileNo":request.body.hidMobile,
    "SubscriberPrefCategory":request.body.ServiceProvider,
    "PreferenceDate": today,
    "PreferenceStartTime":request.body.datetimepicker3,
    "PreferenceEndTime":request.body.datetimepicker4
    }
    });
    request.body = json_blob;
    route.invoke(request,reply);
    reply.write('<script>setTimeout(function () { window.location.href = "/view-dnd-registration.html"; }, 3000);</script>');
})

popup.html:

<html>
   <head>
   </head>
   <body>
      //contains the design for popup
      <script>
         function open_popup(){
         alert("calling open_popup");
         $("html, body").animate({ scrollTop: 0 }, "slow");
         openpopup(); 
         }
      </script>
      <script>
         $("html, body").animate({ scrollTop: 0 }, "slow"); 
         function openpopup()
         {
         alert("calling open popup function");
         var id = '#dialog';
         //Get the screen height and width
         var maskHeight = $(document).height();
         var maskWidth = $(window).width();
         //Set heigth and width to mask to fill up the whole screen
         $('#mask').css({'width':maskWidth,'height':maskHeight});
         //transition effect
         $('#mask').fadeIn(500);
         $('#mask').fadeTo("slow",0.9);
         //Get the window height and width
         var winH = $(window).height();
         var winW = $(window).width();     
         //Set the popup window to center
         $(id).css('top',  winH/2-$(id).height()/2);
         $(id).css('left', winW/2-$(id).width()/2);
         //transition effect
         $(id).fadeIn(2000);
         }
      </script>
   </body>
</html>

请帮助我如何在 Node js 中调用函数 openup();

最佳答案

你不能那样做。有两种不同的方式:

  • 使用 Ajax:在包含表单的 HTML 页面中,您可以使用 Ajax 请求来调用 /process-view-dnd-registration 路由。然后,您将处理响应并直接在页面中调用 open_popup() 函数。

类似的事情:

$("#formId").submit(function(e) {
  e.preventDefault();

  var formData = {
     'test': $('input[name=test]').val()
     // All your input values
  };

  $.ajax({
     url: '/process-view-dnd-registration',
     type: 'POST',
     dataType: 'json',
     data: formData
  }).done(function(data) {
     // Call your open_popup() function here
  });
});
  • 在模板中处理响应:您可以在调用 popup.html 模板时在 app.post 方法中发送响应,并向其发送参数,例如 bool 变量,可以是 openPopup,然后在模板页面中处理它以确定是否显示弹出窗口。这取决于您在项目中如何使用模板。

关于javascript - 如何从 Node js app.post() 调用 html 中的 javascript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48484097/

相关文章:

javascript - 边框半径在 IE9 中不起作用

javascript - 非方形透明图标图像的悬停效果

jquery - 自动滚动一个div

JavaScript 函数不会加载 html 页面

javascript - 星级评分系统jquery

javascript - 如何在 Javascript 中定义串联?

javascript - 如何使用 Laravel 上的下拉菜单过滤表格

javascript - 如何使用 jQuery 设置滚动位置?

javascript - 交换表行

html - UIWebView从包中加载html文件,并提供文件信息?