Git Product home page Git Product logo

rest's Introduction

rest for jQuery

How to use:

Rest API

获取用户信息

  • GET /users?limit=10&sort=id&order=asc
  • 输出: Content-Type: application/json
[
		{
			"id": 1,
			"username": "user1"
		}, 
		{
			"id": 2,
			"username": "user2"
		}, ...
]

用户登录

  • POST /login
  • 输入:
    Content-Type: application/json
{
		"username": "user1",
		"password": "password1"
}
  • 输出:
    HTTP/1.1 200 OK
    HTTP/1.1 401 Unauthorized

修改用户

  • PUT /user/:id
  • 输入:
    Content-Type: application/json
{
		"email": "[email protected]"
}
  • 输出:
    HTTP/1.1 200 OK

删除用户

  • DELETE /user/:id
  • 输出:
    HTTP/1.1 200 OK

JavaScript for Rest API

引用 jquery 和 rest.js 文件

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="rest.js"></script>

定义 Rest

var methods = ['get:/users', 'post:/login', 'put:/user', 'delete:/user'];
var rest = new Rest(methods);
var rest = new Rest('/path', methods);//baseurl: '/path'

GET

//获取用户信息: rest.getUsers(callback);
rest.getUsers(function(status, users) {
	console.log(status);//200 or 401
	console.log(users);//user list
});
//根据条件获取用户信息: rest.getUsers(callback, params);
rest.getUsers(function(status, users) {
	console.log(status);//200 or 401
	console.log(users);//user list
}, {
	limit: 10,
	sort: 'id',
	order: 'asc'
});

POST

//登录: rest.postLogin(callback, params);
rest.postLogin(function(status, users) {
	console.log(status);//200 or 401
}, {
	username: 'user1',
	password: 'password1'
});

PUT

//修改用户: rest.putUser(callback, id, params);
rest.putUser(function(status) {
	console.log(status);
}, 1, {
	email: '[email protected]'
});

DELETE

//删除用户: rest.deleteUser(callback, id);
rest.deleteUser(function(status) {
	console.log(status);
}, 1);

支持多级 url

var method = ['get:/path/root/test', 'get:/path/root/hello', ];
var rest = new Rest(method);
rest.path.root.getTest(function(status, data) {
	console.log(status, data);
});
rest.path.root.getHello(function(status) {
	console.log(status, data);
});

author:

rest's People

Contributors

wenzhixin avatar

Watchers

James Cloos avatar lionMac avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.