weixin_33696106 2012-02-24 20:09 采纳率: 0%
浏览 20

小书签中的Ajax请求

I want to build a bookmarklet that will allow me to send the URL of the current page that I am in, to a php file, get a confirmation response and display it to the user.

I tried several things, and only one method worked, it succeeded in sending the request, but its not displaying the response.

javascript: (function (e, a, g, h, f, c, b, d) {
    if (!(f = e.jQuery) || g > f.fn.jquery || h(f)) {
        c = a.createElement("script");
        c.type = "text/javascript";
        c.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + g + "/jquery.min.js";
        c.onload = c.onreadystatechange = function () {
            if (!b && (!(d = this.readyState) || d == "loaded" || d == "complete")) {
                h((f = e.jQuery).noConflict(1), b = 1);
                f(c).remove()
            }
        };
        a.documentElement.childNodes[0].appendChild(c)
    }
})(window, document, "1.3.2", function ($, L) {
    $.get("http://mysite.com/recommend.php", {
        url: encodeURIComponent(document.URL)
    }, function (data) {
        if (data.error) {
            alert('Looks like someone else added this site just before you did, Thank you though!');
        } else {
            alert(document.URL + ' successfully added!');
        }
    }, 'json');
});

Is there anyway I can get this to work ? I read somewhere about origin policies - Is there any other way to achieve what I am trying to do - Objective is to build a bookmarklet, communicate to the server and display the response.

  • 写回答

1条回答 默认 最新

  • weixin_33709590 2012-02-25 17:31
    关注

    You can't use standard Ajax cross domain. Browsers enforce "same domain" policy. You must use JSONP. http://en.wikipedia.org/wiki/JSONP

    Search this page for "jsonp" to get started on how to use jQuery with JSONP: http://api.jquery.com/jQuery.ajax/

    A more simple solution could be this this:

    var recURL='http://mysite/com/recommend.php?url=' 
      + encodeURIComponent(document.URL);
    document.body.appendChild(document.createElement('script')).src=recURL;
    

    recommend.php should return Javascript code like: alert(document.URL + ' successfully added!');

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作