Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax call from Bookmarklet

I am trying to create a bookmarklet that, upon clicking, would request some information from the user (a url and a couple other fields in this case) and then send that data to a php page on my server and then display the result.

I would like to do an Ajax call for this so that I don't actually redirect to the new page, just get the data but I assume I would run into the "Same Origin Policy" limitation of Ajax.... is there any known way of basically doing the same thing?

Also, what would be the best way to pass the parameters? I already have a mechanism in place to recieve the parameters as a post message from a form...is there any way I could just reuse this?

like image 763
Adam Haile Avatar asked Mar 20 '09 01:03

Adam Haile


1 Answers

You can set a bookmarklet by create a bookmark and add that piece of code below in location, but, according to same origin policy limitation, that will only work when the current tab is on the same location, here www.google.com.

If I've understand well your needs, that should be ok for your problem.

var request = new XMLHttpRequest();
request.open("GET", "http://www.google.com", true);
request.onreadystatechange = function() {
  var done = 4, ok = 200;
  if (request.readyState == done && request.status == ok) {
    if (request.responseText) {
      alert(request.responseText);
    }
  }
};
request.send(null);

I don't know if POST would work.

like image 183
paulgreg Avatar answered Nov 15 '22 21:11

paulgreg