JQuery and Ajax call Sina API to obtain the code for a short URL

Source: Internet
Author: User

JQuery and Ajax call Sina API to obtain the code for a short URL

This article describes how to call Sina API to obtain short URLs. In this example, we will introduce how to use JQuery and Ajax. For more information, see

The Code is as follows:

<! Doctype html>

<Html>

<Head>

<Meta charset = "UTF-8">

<Title> Get Sina short website </title>

<Script src = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"> </script>

<! -- <Script type = "text/javascript" src = "js/jquery-2.1.0.min.js"> </script> -->

</Head>

<Body>

<Label for = "long"> long URL: </label>

<Input value = "http://baike.baidu.com/link? Url = XLrVdYRThMvU_DlDT67v2wg9XYgG3xXvsB97WZFDz4psPORqGotkTiH1O5L1SGrD9Du-OSxWt_5E2KNtk01HhK "id =" long "> <br>

<Label for = "app_key"> enter your app_key (you can search for it online): </label> <br>

<Input value = "211160679" id = "app_key"> <br>

<Span id = "short"> short URL: </span> <br>

<Span id = "dlgcy"> call address: </span> <br>

<Button type = "button" id = "btn"> Click me </button> <br>

<! -- <Span id = "link"> URL: </span> -->

<Br>

<A href = "http://www.cnblogs.com/azure/archive/2012/08/29/WeiboAPI.html" target = "_ blank"> reference: using short-chain services as an example, this paper discusses the AppKey-free, authentication-free, Ajax cross-origin call of Sina Weibo API </a>

<Br>

<A href = "http://open.weibo.com/tools/console? Uri = short_url/shorten & httpmethod = GET & key1 = url_long & value1 = http % 3A % 2F % 2Fopen.weibo.com % 2Fqa "target =" _ blank "> Sina API test </a>

</Body>

 

<Script type = "text/javascript">

$ (Document). ready (function (){

$ ("# Btn"). click (function (){

Var short = $ ("# short ");

Var long = $ ("# long"). val (); // get the attribute value;

// Var url = "https://api.weibo.com/2/short_url/shorten.json"; // official API address;

Var url2 = "http://api.weibo.com/2/short_url/shorten.json ";

Var app_key = $ ("# app_key"). val (); // invalid app_key may cause no response;

// Var access_token;

// Var cmd = url + "& url_long =" + long + "& access_token =" + access_token;

Var cmd2 = url2 + "? Source = "+ app_key +" & url_long = "+ long;

 

Var message = "";

$. Ajax ({// underlying method;

Url: cmd2,

Type: "GET ",

DataType: "jsonp", // use the JSONP Method for AJAX. json has cross-origin issues;

Cache: false,

Success: function (data, status ){

// Obtain the returned information;

For (x in data. data. urls [0]) message + = x + '=' + data. data. urls [0] [x] + '&';

Alert ("Data: n" + message + "nnStatus:" + status );

Short. append (data. data. urls [0]. url_short + "<br> ");

// $ ("# Dlgcy"). hide ();

$ ("# Dlgcy"). text (cmd2 );

},

Error: function (obj, info, errObj ){

Alert ("$. ajax () error:" + info );

}

});

 

});

});

</Script>

</Html>

 

Code download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.