weixin_33696106
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.

图片转代码服务由CSDN问答提供 功能建议

                    

我想构建一个书签,使我可以将当​​前所在页面的URL发送到php文件中,获得确认响应并将其显示给用户。

我尝试了几种方法,但只有一种方法有效,它成功发送了请求,但未显示响应。

  javascript:(函数(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 ==“已加载” || d ==“完成”)){
                 h((f = e.jQuery).noConflict(1),b = 1);
                 f(c).remove()
             }
         };
         a.documentElement.childNodes [0] .appendChild(c)
     }
 })(窗口,文档,“ 1.3.2”,函数($,L){
     $ .get(“ http://mysite.com/recommend.php”,{
         网址:encodeURIComponent(document.URL)
     },函数(数据){
         如果(data.error){
             alert('像其他人一样在您添加之前添加了此站点,尽管如此,谢谢!');
         }其他{
             alert(document.URL +'已成功添加!');
         }
     },'json');
 });
  

 

反正我可以继续工作吗? 我在某处阅读了有关源策略的信息-是否有其他方法可以实现我要尝试的操作-目标是构建书签,与服务器通信并显示响应。     

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • weixin_33709590
    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!');

    点赞 评论

相关推荐