I am trying to make a javascript bookmarklet which will use the HTTP POST method to send data to my php form from my browser. I can't seem to get it working...can someone take a look and tell me why please?
var xhr = new XMLHttpRequest();
var params="v=66b12127";
xhr.open("POST", "http://google.com/index.php", true);
xhr.onreadystatechange = function() {
if(this.readyState == 4) {
window.alert("works");
}
}
xhr.send(params);
In bookmark format:
javascript:var xhr=new XMLHttpRequest();var params="v=66b12127";xhr.open("POST","http://google.com/index.php",true);xhr.onreadystatechange=function(){if(this.readyState==4){window.alert("works");}}xhr.send(params);
In most circumstances, you can't perform a POST request due to cross-domain restrictions (also called Same origin policy). However, if you are only interested in doing the request and not interested in reading the answer, there is a solution: With the bookmarklet, insert a form with the appropriate action attribute, then run the form's DOM object's .submit() function. Note that this will reload the whole page. If you want to avoid that, create an iframe as well and set the form's target attribute to its name. You can either choose to hide the iframe or—if you want the user to see the result—leave it visible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With