简体   繁体   中英

Permission denied when trying to POST information to some other page?

I am using the following to POST information, and when I am creating the form element, I get an exception: "Permission denied".

var newWindow = window.open('', '_blank', 'alwaysRaised=yes,toolbars=no, menubar=no, location=no, scrollbars=yes, resizable=yes, status=yes,left=10000, top=10000, width=10, height=10, visible=none', '');

var tempFormElement = newWindow.document.createElement('form');
tempFormElement.method = 'post';
tempFormElement.action = urlToOpen;

var tempInputElement;
tempInputElement = newWindow.document.createElement('input');
tempInputElement.setAttribute('TPLInfo', JSON.stringify(linkageInfo));
tempFormElement.appendChild(tempInputElement);
tempFormElement.submit();

newWindow.document.body.removeChild(tempFormElement);
newWindow.close();

Please suggest.

  1. Due to security restrictions, most browsers nowadays will not allow you to create a new window outside the view port or smaller than 100x100
  2. you do not need to open new windows - instead use AJAX
  3. If you cannot use AJAX because of urlToOpen is on another domain, you could not have closed the window or removed the tempform element anyway.
  4. you could not do that on the same origin either since it is no longer there after submission
  5. there is no valid property of an input element called TPLInfo

So I suggest - if you can:

Ajax using jQuery

$.post(urlToOpen,{"TPLInfo":JSON.stringify(linkageInfo)});

or if not on same domain (plain JS) :

Live Demo

var newWindow;
var urlToOpen = "....";
function perform() {
      if (!newWindow) { 
        alert("Sorry, you must allow popups");
        return;
      }
      var tempFormElement = newWindow.document.createElement('form');
      tempFormElement.method = 'post';
      tempFormElement.action = urlToOpen;
      var tempInputElement;
      tempInputElement = newWindow.document.createElement('input');
      tempInputElement.setAttribute("name","TPLInfo");
      tempInputElement.value = JSON.stringify({"a":"1"});
      tempFormElement.appendChild(tempInputElement);
      newWindow.document.body.appendChild(tempFormElement);
      tempFormElement.submit();
}
window.onload=function() {
    document.getElementById("but").onclick=function() {
//   var parms = "alwaysRaised=yes,toolbars=no,menubar=no,location=no,scrollbars=yes,resizable=yes,status=yes,left=10000,top=10000,width=10,height=10,visible=none";
      // valid parms, yes I know you want to hide and close but nope.
      var parms = "scrollbars,resizable,status,left=10000,top=10000,width=100,height=100";

      newWindow = window.open('', '_blank',parms);
      setTimeout(perform,500);  // give IE time to get over opening
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM