Ajax bookmark call

I am trying to create a bookmarklet that, after clicking, asks the user for some information from the user (URL and a couple of other fields in this case), and then sends this data to the php page on my server, and then displays the result.

I would like to make an Ajax call for this, so that I don’t actually get redirected to a new page, I just get the data, but I assume that I came across an Ajax restriction of "Same origin" .... is there any known way in basically do the same?

Also, what would be the best way to pass parameters? I already have a mechanism for getting parameters as a message from a form ... is there a way I could just reuse it?

+7
javascript ajax bookmarklet
source share
2 answers

You can set the bookmarklet by creating a bookmark and adding this code snippet below in the location, but, according to the same restriction of the origin policy , which will work only when the current tab is in the same place, here www.google.com .

If I understand your needs well, this should be good 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 will work.

+6
source share

You cannot make a message, but GET will work fine. If you use something like jQuery, it will simply create a script tag with the src url that will send the data you want to send.

You will need to return the JSON style data.

See: http://docs.jquery.com/Ajax/jQuery.getJSON

Alternatively, your bookmarklet can create an iframe on the page, and this can make you work with sending data (you can use post then) if you do not want to communicate between the iframe and the page itself, but instead just use user input to send it.

+1
source share

All Articles