小书签中的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.

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