I am trying to write a Bookmarklet and the goal is to be able to submit information from any site X (the origin page they are on when clicking the开发者_如何学Python bookmarklet) to my site's servers while staying on site X.
Ideally, I would be able to send a response back and have it pop up somewhere but this is not necessary.
I keep running into the issue of the same origin policy -- that from site X, XMLHttpRequests can only be initiated with site X's domain.
Does anyone know of a way around this (or a tutorial they can point me to)? some Ajax with a Bookmarklet?
Thanks so much!
You can perform cross domain ajax request (send using GET and receive the data as JSON) using JSONP.
The bookmarklet that you load can POST data to your server from the host page. This is strange, but only Javascript is restricted by the Same Origin Policy.
Making GET
or POST
calls to another server work fine.
The bookmarklet can inject a hidden IFRAME
in the host page , with a src
attribute like http://yourdomain.com/listen
.
Then build a FORM
with the attribute:target
pointing to that IFRAME
.
And finally submit the form to POST
the data.
The sad news, is SOP won't let you read the response of the POST directly, as it happens in the IFRAME and has another domain than the host page.
But if you need a feedback of the request, your bookmarklet can use setInterval
to poll every X milliseconds and ask the status of the request, using JSONP.
Calling something like:
http://yourdomain.com/get-post-status?id=2234234&callback=showResult
精彩评论