Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 去邮局不工作_Javascript_Http_Post_Get - Fatal编程技术网

Javascript 去邮局不工作

Javascript 去邮局不工作,javascript,http,post,get,Javascript,Http,Post,Get,我想请求发布以下代码的请求- <html> <head> <script language="JavaScript"> function redirect() { if (window.focus) self.focus(); this.location = '/test/GetReports?id=

我想请求发布以下代码的请求-

       <html>
       <head>
         <script language="JavaScript">
          function redirect()   {
             if (window.focus)
           self.focus();
                this.location = '/test/GetReports?id=       
           <%=System.currentTimeMillis()+session.getId()%>';
         }
      </script>
    <title>Downloading Document</title>
         </head>
   <body marginwidth='0' marginheight='0' onload='javascript:redirect()'>
   </body>
 </html>

函数重定向(){
if(window.focus)
self.focus();
this.location='/test/GetReports?id=
';
}
下载文件
为了实现这一点,我做了以下工作-

   <html>
   <head>
     <script language="JavaScript">
     function redirect()    {
     if (window.focus)
        self.focus();
      location =  '/test/GetReports?id=<%=System.currentTimeMillis()+session.getId()%>';
      var form = document.createElement("form");
      form.setAttribute("method", "post");
      form.setAttribute("action", location);
       form.submit();
      }
   </script>
   <title>Downloading Document</title>
   </head>
    <body marginwidth='0' marginheight='0' onload='javascript:redirect()'>
  </body>
 </html>

函数重定向(){
if(window.focus)
self.focus();
位置='/test/GetReports?id=';
var form=document.createElement(“表单”);
form.setAttribute(“方法”、“帖子”);
form.setAttribute(“动作”,位置);
表单提交();
}
下载文件
但这没有任何区别。这个请求仍然是Get。请告诉我还需要做什么


谢谢你的帮助

您实际上是在用POST表单发送GET数据。URL中指定的参数始终被视为获取数据

为了说明这意味着什么,如果您有一个如下所示的表单:

<form method="post" action="test.html?gid=1">
    <input name="pid" value="2">
</form>

还要注意,我将
location
重命名为
loc
——否则您可能会遇到问题,因为
location
将指向
窗口。location

测试此
表单.setAttribute(“方法”,“帖子”),然后查看它是否有效。只需指出,script元素上的
language
属性已过时。您应该改用
type=“text/javascript”
。第二次尝试是否有效?我不知道它是如何发送GET请求的,我已经用你的代码测试过了,效果很好:)唯一的区别是我的位置指向了另一个位置。谢谢大家的帮助!我尝试了上面的代码,但是redirect()函数本身没有得到执行。上面的jsp是从一个servlet调用的,这个servlet正在从JS打开一个新的弹出窗口。下面的代码片段正在打开新窗口,然后调用上面的jsp。var f=document.getElementById('Form1');loc='/ocs/Displayservlet?AcctNum='+AcctNum+';f、 setAttribute(“方法”、“帖子”);f、 行动=loc;打开(“”,'TheNewWindow','width=640,height=480,toolbar=no);f、 提交();所以这个servlet调用是post,但是当我尝试上面的代码时,redirect()就不起作用了。有什么线索吗?有人能解释一下吗?这与最初的问题完全无关。请将其作为新问题发布。我能够解决该问题。我必须添加document.body.appendChild(表单);双引号缺少输入;谢谢你的帮助!
function redirect() {
    if (window.focus) self.focus();
    loc = '/test/GetReports';   // note: no parameters here
    var form = document.createElement("form");

    // create the input element
    var input = document.createElement("input");
    input.setAttribute("name", "id");
    input.setAttribute("value", <%=System.currentTimeMillis()+session.getId()%>);
    form.appendChild(input);

    form.setAttribute("method", "post");
    form.setAttribute("action", loc);
    form.submit();
}