您的位置:首页 > Web前端 > JavaScript

JavaScript端构建RESTFul Client的简单例子

2017-08-26 18:13 375 查看
本文由Markdown语法编辑器编辑完成。

1. REST服务:

REST, which is named from “Representational State Transfer,” has the following attributes:

(1) REST is an architectural style rather than a clearly defined format. Although REST is frequently implemented using HTTP for transporting messages and JSON for passing data, you can also pass data as XML or plain text. REST makes use of existing web standards such as HTTP, URL, XML, and JSON.

(2) REST is resource oriented. Typically a resource is identified by a URL and uses operations based explicitly on HTTP methods, such as GET, POST, PUT, and DELETE.

(3) REST typically has a small overhead. Although it can use XML to describe data, it typically uses JSON which is a light-weight data wrapper. JSON identifies data with tags but the tags are not specified in a formal schema definition and do not have explicit data types.

翻译:

REST, 名称源于“表现层状态转化”,具有以下的几项属性:

(1)REST是一种架构形式而不是一个明确定义的格式。尽管REST通常用HTTP来传输消息和用JSON来传输数据,你也可以用XML或纯文本来传输数据。REST利用现有的Web标准,如HTTP, URL, XML和JSON。

(2)REST是基于资源的。通常,资源由URL来标识,并使用基于HTTP的方法,如GET, POST, PUT和DELETE来进行操作。

(3)REST通常只有很小的开销。虽然它可以使用XML来描述数据,但它通常用JSON, 一种轻量级的数据包装器。JSON使用标签来标识,但是这些标签并没有以正式的语法定义,也没有明确的类型。

以上内容来自链接:

http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=EREST_overview

扩展阅读可参考链接:

阮一峰《理解RESTful架构》

http://www.ruanyifeng.com/blog/2011/09/restful.html

RESTful API设计指南:

http://www.ruanyifeng.com/blog/2014/05/restful_api.html

2. REST客户端:

testRestfulClient.html文件内容为:

<!DOCTYPE html>
<html>
<head>
<title>RESTFul Client test page</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<script src="./jquery-2.1.3.min.js"></script>
<script src="./testRestfulClient.js"></script>

<script>
document.write("<h1>Send Restful Get and Post:</h1>");
</script>
<div>
<input id="sendRestfulGet" onclick="sendRestfulGet();" type="button" value="SendGet">
</br>
</div>
</body>
</html>


testRestfulClient.js文件内容为:

function sendRestfulGet(){
var doubanURL = 'https://api.douban.com//v2/movie/top250';
$.ajax({
url: doubanURL,
type: 'Get',
data: '',
dataType: 'JSONP',
crossDomain: true,
success: function(data){
console.log(data);
}
});
}


返回结果:



而以下是进入豆瓣网站,选择豆瓣电影TOP250选项后(链接:https://movie.douban.com/top250),得到的电影列表。通过比较可以看出排在前10位的电影为:

(1)肖申克的救赎

(2)霸王别姬

(3)这个杀手不太冷

(4)阿甘正传

(5)美丽人生

(6)千与千寻

(7)辛德勒的名单

(8)泰坦尼克号

(9)盗梦空间

(10)机器人总动员

排名前10的电影我都已经看过很多遍了,而《肖申克的救赎》是给我最大震撼的。希望大家有时间可以欣赏一下这十部电影。相信品牌的力量,相信群众的眼光!







参考链接:

解决Ajax无法跨越的问题:

http://blog.csdn.net/zhoucheng05_13/article/details/53580683

http://www.cnblogs.com/lastnigtic/p/6486331.html

基于node.js创建REST客户端的例子:

https://www.npmjs.com/package/node-rest-client
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: