来自 Bookmarklet 的 Ajax 调用
我正在尝试创建一个小书签,单击后会向用户请求一些信息(在本例中为 url 和其他几个字段),然后将该数据发送到我的服务器上的 php 页面,然后显示结果。
我想为此进行 Ajax 调用,这样我实际上不会重定向到新页面,只是获取数据,但我假设我会遇到 Ajax 的“同源策略”限制......有没有什么基本上做同样事情的已知方法?
另外,传递参数的最佳方式是什么? 我已经有了一个机制来从表单中接收参数作为发布消息...有什么方法可以重用它吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过创建书签并在下面的位置添加该代码段来设置小书签,但是,根据 同源政策限制,仅当当前标签位于同一位置(此处为 www.google.com。
如果我很了解您的需求,那应该可以解决您的问题。
我不知道 POST 是否有效。
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.
I don't know if POST would work.
您将无法发帖,但 GET 可以正常工作。 如果您使用 jQuery 之类的东西,它只会创建一个带有 src URL 的脚本标记,该标记将发送您要提交的数据。
您必须返回 JSON 样式的数据。
请参阅: http://docs.jquery.com/Ajax/jQuery.getJSON
或者,您的小书签可以在页面上创建一个 iframe,如果您不想在 iframe 和页面本身之间进行通信,而只是使用用户输入,那么您可以完成提交数据的工作(然后可以使用 post)提交。
You won't be able to do a post, but a GET will work fine. If you're using something like jQuery, it will simply create a script tag with a src URL which would send the data you are looking to submit.
You will have to return JSON style data.
See: http://docs.jquery.com/Ajax/jQuery.getJSON
Alternatively, your bookmarklet could create an iframe on the page, and that could do you work of submitting the data (you could use post then) if you weren't looking to communicate between the iframe and the page itself, but instead just use user input to submit.