说明
jax 是一个处理 AJAX 请求和响应的库。 它没有捆绑任何命令。 这只是一组模块.
安装
jax 可作为 npm 软件包。 您可以通过 npm 软件包安装 @ephox/jax
从 npm 安装
npm install @ephox/jax。 。 。 .
使用 API
jax 支持几种方法、内容类型和响应类型。 更具体地说,
方法: 获取、 POST、 PUT、 DELETE 内容类型: 无, 形式, json, plain, html 反应类型:json、blob、文本、html、xml
还有, credentials 可随请求而发送。 更多信息可以找到 这里。 。 。 .
这个 ephox.jax.api.Ajax API提供了提出AJAX请求的各种方法. 他们都回来了 LazyValue (见 卡塔马里),是一个类似承诺的数据结构,带有 get, (中文(简体) ), map,以及 isReady 函数。 APIs 还在结尾处为任何自定义请求头取一个额外的参数,默认为 {} 若不提供则提供.
ContentType, (中文(简体) ), ResponseType,以及 Credentials 使用内部的构造器指定 ephox.jax.api.ContentType, (中文(简体) ), ephox.jax.api.ResponseType,以及 ephox.jax.api.Credentials 页:1.
获取请求
Ajax.get(url, responseType, credentials, _custom)
此点出带有指定响应类型的 GET 请求 。 内容类型以 none。 。 。 .
Ajax.get(
'http://localhost/server/get/1',
ResponseType.json(),
Credentials.none(),
{ }
).get(function (result) {
// result is a result, so you need to fold over it for Err or Succ(x)
result.fold(function (err) {
console.error('Server error', err);
}, function (val) {
console.log('Get response', val);
})
});
后请求
Ajax.post(url, contentType, responseType, credentials, _custom)
此点出 POST 请求, 附带指定的响应类型和内容类型 .
Ajax.post(
'http://localhost/server/post',
ContentType.json({
'send-data': '10'
}),
ResponseType.xml(),
Credentials.none(),
{ }
)).get(function (result) {
// result is a result, so you need to fold over it for Err or Succ(x)
result.fold(function (err) {
console.error('Server error', err);
}, function (xml) {
console.log('Post response', xml);
})
});
PUT 请求
Ajax.put(url, contentType, responseType, credentials, _custom)
此点出 PUT 请求, 包含指定的响应类型和内容类型 .
Ajax.put(
'http://localhost/server/put',
ContentType.json({
'send-data': '10'
}),
ResponseType.json(),
Credentials.none(),
{ }
)).get(function (result) {
// result is a result, so you need to fold over it for Err or Succ(x)
result.fold(function (err) {
console.error('Server error', err);
}, function (val) {
console.log('Put response', val);
})
});
DELETE 请求
Ajax.del(url, responseType, credentials, _custom)
此点出带有指定响应类型的 DELETE 请求 .
Ajax.get(
'http://localhost/server/del/1',
ResponseType.json(),
Credentials.none(),
{ }
).get(function (result) {
// result is a result, so you need to fold over it for Err or Succ(x)
result.fold(function (err) {
console.error('Server error', err);
}, function (val) {
console.log('Delete response', val);
})
});
运行测试
$ yarn test
这些测试要求 基石 和铬.