开发者

jQuery cross-domain posting into rails website

I'm currently developing a small rails application that allows me to manage m开发者_开发技巧y personal bookmarks, something along the lines of delicious meets instapaper and for the first time, I'm facing the limitations of cross-domain form posting which I'm not exactly sure how to overcome.

While browsing any website, I can press a bookmark which injects jQuery (1.5) and a custom HTML form into the current DOM. After filling and submitting the previously injected form, I get redirected to my rails website, where a successful confirmation is displayed.

My problem starts when I try to avoid the redirect by replacing the original form workflow with an Ajax request. Apparently a same origin policy is being enforced by the browser, restricting me to POST any data into my remote website.

Some developers have suggested me to proxy the requests or to start rendering the form inside an iframe. Regarding the proxy solution, I honestly don't have a clue on how to implement this with rails. The iframe looks doable but I'm not sure about how easy it would be to access/work/update from within the DOM.

Thanks in advance!


Don't let working with the iframe scare you. It's easy to access from the parent via:

  • window.frame.frame[0]... //continue your call here, assuming target iframe is the first/only frame on the page.

or

  • window.frame.frameid... ///continue your call here.

or with jQuery:

  • $("#iFrame").contents().find("#someDiv") //continue your call here.


With the iFrame approach, you use javascript to get the parent.window (I believe) url. I am not sure how much more DOM access you have.

You could also try to look at the AJAX side of jQuery: Ajax API - jQuery. This would allow you to post to a script (I am fairly sure I used it cross-domain a few years back). With this, you could have the script return a code to the form on the page and not have to redirect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜