Javascript – Ajax call from Bookmarklet

ajaxbookmarkletjavascript

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?

Best Solution

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.