通过jQuery Ajax调用创建TinyURL的jQuery、Ajax、TinyURL

2023-09-10 13:58:51 作者:灵魂操控者

我通过SO simiilar问题看,但似乎无法找到一个解决什么似乎是一个简单的电话。

I've looked through simiilar questions on SO, but can't seem to find one addressing what seems like a simple call..

function TweetThis(url)
{
    $.ajax({
      url: "http://tinyurl.com/api-create.php?url=" + url,
      cache: false,
      success: function(data){
       alert(data);
      }
    });
}

基本上,我想打电话给TinyURL的使用Ajax调用和一个长的URL并返回缩短的URL ..成功永远不会触发,但是当我查询的网址它建立在一个浏览器返回的罚款。

Basically I want to call TinyURL with an Ajax call and a long URL and return the shortened URL.. The success never fires, but when I check the URL it builds it returns fine in a browser.

在看萤火虫它不显示响应回来了......我缺少什么?

Looking in Firebug it doesn't show response coming back.. what am I missing?

推荐答案

试图进行常规AJAX请求是因为的同源策略的限制。 幸运的是有一个 JSONP 的 API 礼貌的雷米夏普。

Attempting to make a regular AJAX request is impossible because of same origin policy restrictions. Luckily there's a JSONP API courtesy of Remy Sharp.

下面的工作code:

function TweetThis(bigurl)
{
    $.getJSON(
      "http://json-tinyurl.appspot.com/?&callback=?",
      {url: bigurl},
      function(data){
       alert(data.tinyurl);
      }
    );
}