通过 Jquery Ajax 调用创建 TinyURL

发布于 2024-08-04 17:55:41 字数 385 浏览 8 评论 0原文

我已经浏览过类似的问题,但似乎找不到一个解决看似简单的调用的问题。

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

基本上我想使用 Ajax 调用和长 URL 来调用 TinyURL 并返回缩短的 URL。成功永远不会触发,但是当我检查它构建的 URL 时,它在浏览器中返回正常。

在 Firebug 中查看它没有显示返回的响应..我错过了什么?

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);
      }
    });
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

忘你却要生生世世 2024-08-11 17:55:41

由于 同源,尝试发出常规 AJAX 请求是不可能的政策限制。
幸运的是,有一个
JSONP API雷米·夏普.

这是工作代码:

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

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.

Here's working code:

function TweetThis(bigurl)
{
    $.getJSON(
      "http://json-tinyurl.appspot.com/?&callback=?",
      {url: bigurl},
      function(data){
       alert(data.tinyurl);
      }
    );
}
凉墨 2024-08-11 17:55:41

从未使用过它,但也许值得一试。
http://tiny-url.info/open_api.html

如果您有能力添加逻辑在服务器端,您可以通过安装“shim”或网关脚本来避免对 JSONP 的要求,该脚本可以执行您想要的操作并返回格式化的 JSON 字符串。

通过调用tinyurl.com的API生成tinyurl的一些脚本示例:

任何人都可以获取该代码并自行托管以允许网页获得访问权限到tinyurl服务。同样的方法适用于任何无法通过 JSONP 访问的服务。

never used it, but maybe worth checking out.
http://tiny-url.info/open_api.html

If you have ability to add logic to the server side, you can avoid the requirement for JSONP by installing a "shim" or gateway script that does what you want, and returns a formatted JSON string.

some examples of scripts that produce tinyurls by calling tinyurl.com's API:

Anyone can take that code and host it themselves to allow webpages to gain access to the tinyurl service. The same approach works for any service that is not accessible via JSONP.

揽月 2024-08-11 17:55:41

在 Safari 4 (Mac OS X) 中,它工作正常。
在 Firefox 3 (Mac OS X) 中,它只工作了一半 - 出现一个 alert 对话框,但它是空的(因此 success 正在触发,但没有返回任何数据)。< br>
这似乎是 Firefox 的一个错误。

In Safari 4 (Mac OS X), it works fine.
In Firefox 3 (Mac OS X), it half works - a alert dialog comes up, but it's empty (so success is firing, but no data is returned).
It seems to be a Firefox bug then.

新一帅帅 2024-08-11 17:55:41

试试这个。

脚本:

<script language="javascript" type="text/javascript">
<!-- 
var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser is very old!");
            }
        }
    }

//Browser Support Code
function ajaxGetTiny(){

    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('ajaxDisplayTiny');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;

        }
    }
        var long_url = document.getElementById('long_url').value;


    var queryString = "?long_url=" + long_url;
    ajaxRequest.open("GET", "getTiny.php" + queryString, true);
    ajaxRequest.send(null); 

}

function ClipBoard() 
{
holdtext.innerText = copytext.innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
}
//-->

</script>

现在是形式:

<form name='myForm'>
      <input name="long_url" type="text" class="main" size="90">
      <br>
        <input type='button' class="Buttons" onclick='ajaxGetTiny();' value='GET TINY' />
      </form>

现在是帮助文件:

<? 
//gets the data from a URL  
function get_tiny_url($url)  {  
    $ch = curl_init();  
    $timeout = 5;  
    curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);  
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);  
    $data = curl_exec($ch);  
    curl_close($ch);  
    return $data;  
}

//test it out!
$new_url = get_tiny_url($_GET['long_url']);

?>
<link href="../styles.css" rel="stylesheet" type="text/css" />


<table width="100%" border="0" class="main">
<tr>
            <td width="5%" align="left" valign="middle"><strong>longURL:</strong></td>
            <td width="95%" valign="middle" class="ArticleBody"><? echo $_GET['long_url']; ?></td>
  </tr>
          <tr>
            <td align="left" valign="middle"><strong>tinyURL:</strong></td>
            <td valign="middle" class="ArticleBody"><SPAN ID="copytext"><? echo $new_url; ?></SPAN> 
            <TEXTAREA ID="holdtext" STYLE="display:none;"></TEXTAREA><br>
            <BUTTON onClick="ClipBoard();">Copy to Clipboard</BUTTON>
</td>
  </tr>
        </table>

Try this one.

The script:

<script language="javascript" type="text/javascript">
<!-- 
var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser is very old!");
            }
        }
    }

//Browser Support Code
function ajaxGetTiny(){

    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('ajaxDisplayTiny');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;

        }
    }
        var long_url = document.getElementById('long_url').value;


    var queryString = "?long_url=" + long_url;
    ajaxRequest.open("GET", "getTiny.php" + queryString, true);
    ajaxRequest.send(null); 

}

function ClipBoard() 
{
holdtext.innerText = copytext.innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
}
//-->

</script>

Now the form:

<form name='myForm'>
      <input name="long_url" type="text" class="main" size="90">
      <br>
        <input type='button' class="Buttons" onclick='ajaxGetTiny();' value='GET TINY' />
      </form>

Now the helper file:

<? 
//gets the data from a URL  
function get_tiny_url($url)  {  
    $ch = curl_init();  
    $timeout = 5;  
    curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);  
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);  
    $data = curl_exec($ch);  
    curl_close($ch);  
    return $data;  
}

//test it out!
$new_url = get_tiny_url($_GET['long_url']);

?>
<link href="../styles.css" rel="stylesheet" type="text/css" />


<table width="100%" border="0" class="main">
<tr>
            <td width="5%" align="left" valign="middle"><strong>longURL:</strong></td>
            <td width="95%" valign="middle" class="ArticleBody"><? echo $_GET['long_url']; ?></td>
  </tr>
          <tr>
            <td align="left" valign="middle"><strong>tinyURL:</strong></td>
            <td valign="middle" class="ArticleBody"><SPAN ID="copytext"><? echo $new_url; ?></SPAN> 
            <TEXTAREA ID="holdtext" STYLE="display:none;"></TEXTAREA><br>
            <BUTTON onClick="ClipBoard();">Copy to Clipboard</BUTTON>
</td>
  </tr>
        </table>
执手闯天涯 2024-08-11 17:55:41

您是否尝试过将 &callback=? 添加到 URL 末尾?这可能是浏览器安全性造成的阻碍。

Have you tried adding &callback=? to the end of the URL? It could be the browser security getting in the way.

可可 2024-08-11 17:55:41

这应该有效

function TweetThis(url){
    $.get(
        "http://tinyurl.com/api-create.php",
        {url: url},
        function(data){
            alert(data);
        }
    );
}

This should work

function TweetThis(url){
    $.get(
        "http://tinyurl.com/api-create.php",
        {url: url},
        function(data){
            alert(data);
        }
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文