• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

galeone/sa: Simple Ajax: a lightweight library to make AJAX requests

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

galeone/sa

开源软件地址:

https://github.com/galeone/sa

开源编程语言:

JavaScript 100.0%

开源软件介绍:

sa - Simple AJAX

The aim of sa is to provide a lightweight library to easy make AJAX requests.

Learning by examples

Instantiate the AJAX object

var ajax = null;
try {
  ajax = new AJAX();
} catch(e) {
  // handle error (XMLHttpRequest object not supported)
}

Instantiate the AJAX object for a CORS request

var ajax = null;
try {
  var CORS = true;
  ajax = new AJAX(CORS);
} catch(e) {
  // handle error (XMLHttpRequest object not supported)
}

GET request

ajax.get('/somepage?parmeter=wat&who=yello',function(data) {
  // handle completed get request
},
function(statusCode) { // Handle failure
  console.log(statusCode);
});

POST request

ajax.post('/somepage', function(data) {
  // Handle completed post request
}, function(statusCode) { // Handle failure
  console.log(statusCode);
}, parameters);

GET Request returning JSON

ajax.getJSON('/somepage?parmeter=wat&who=yello',function(data) {
  // Handle json return object, like:
  console.log(data.field1, data.field2);
}, function(statusCode) { // Handle failure
  console.log(statusCode);
});

Generic request

You can build your own request.

ajax.request({
url: '/wow',
  type: 'post',
  dataType: 'json',
  data: {wow: 'amazing', 'param2': 1},
  success: function(json) {
    alert(json.responseField2);
  },
  failure: function(statusCode) {
    alert("Request failed with status code: " + statusCode);
  }
});

In the example above we do a POST request to /wow and we expect to obtain a JSON object in respose.

We could specify JSON or XML for the expected format of the response. Empty field means HTML.

Parameters

As you can see from the examples, you can use JSON object or a literal string to pass parameters.

To specify other parameters in AJAX.request you have to follow the definition below.

//define generic ajax request parameter
{
  type: '',
  url: '',
  data: '',
  dataType: '',
  success: function(data){},
  failure: function(errorCode){}
};

With:

  • type = get|post
  • url = whatever you want
  • data: string|JSON
  • dataType: "JSON"|"XML"|""
  • success = function(data) {}
  • failure = function(errorCode) {}

License

sa is licensed under the terms of MIT licence.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Sponsor @galeone on GitHub Sponsors · GitHub发布时间:2022-03-31
下一篇:
Code-the-Dream-School/Backend-rails-ajax发布时间:2022-03-31
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap