1

I would like to make a bookmarklet that users can add to click on while browsing websites. Clicking the bookmarklet grabs some page content and sends it to myserverapi.com

myserverapi.com then sends a reply back to the bookmarklet, which then displays the results to the user (without taking them away from the page they clicked the bookmarklet in). The user confirms something, and then data is once more sent back to myserverapi.com

Is this possible?

I am aware of JSONP but to my knowledge it only works for retrieving data: I was wondering if information can be somehow encoded and sent to the server in a back-and-forth manner.

Thanks!


ah, in this usage case CORS works because I have control of the server. I can't think of a scenario in which back and forth is required to send data to a possibly "un-willing" server anyway, only the other way around.

ejang
  • 3,982
  • 8
  • 44
  • 70

2 Answers2

0

So if you have CORS, why not just post that data to the server? see: How to get a cross-origin resource sharing (CORS) post request working

Community
  • 1
  • 1
mkoryak
  • 57,086
  • 61
  • 201
  • 257
0

This question is answered, but here is answer in case you can't use CORS:

I am aware of JSONP but to my knowledge it only works for retrieving data: I was wondering if information can be somehow encoded and sent to the server in a back-and-forth manner.

You were so close, the answer was right in your grasp.

You SEND data to the server in one of two ways:

1.) The most simple way is with GET. Your JSONP script can be like server/jsonp.php?data=antyhing+you+want. So this is basically the same way as using AJAX communication with the same domain, but instead of using XMLHttpRequest to perform the GET, you are appending a script.

2.) If the data to send exceeds what GET can handle, you can POST the data using a form and an iFrame. One of the form variables should contain a unique ID for that POST to use as a key. Since the POST can not return any data, yo must use the GET method to get the response which corresponds to that key. Because the POST is asynchronous, your GET should poll the server until the server responds with success, which dependent on the server having received a POST with the corresponding key.

DG.
  • 3,417
  • 2
  • 23
  • 28