- Sencha Touch 教程
- Sencha Touch - 主页
- Sencha Touch - 概述
- Sencha Touch - 环境
- Sencha Touch - 命名约定
- Sencha Touch - 建筑
- Sencha Touch - MVC 说明
- Sencha Touch - 第一个应用程序
- Sencha Touch - 构建应用程序
- Sencha Touch - 迁移步骤
- Sencha Touch - 核心概念
- Sencha Touch - 数据
- Sencha Touch - 主题
- Sencha Touch - 设备配置文件
- Sencha Touch - 依赖关系
- 环境检测
- Sencha Touch - 活动
- Sencha Touch - 布局
- Sencha Touch - 历史与支持
- Sencha Touch - 上传和下载
- Sencha Touch - 查看组件
- Sencha Touch - 包装
- Sencha Touch - 最佳实践
- Sencha Touch 有用资源
- Sencha Touch - 快速指南
- Sencha Touch - 有用的资源
- Sencha Touch - 讨论
Sencha Touch - 上传和下载
Sencha Touch 提供 XHR2 配置以与 Ajax 和 Ajax2 开发配合使用。
XHR2是xmlHttpRequest level 2,用于向服务器请求数据。对于任何 Web 应用程序,数据都驻留在服务器上,加载页面后,应借助 Ajax 请求从服务器访问数据。
Sencha Touch 中的 XHR2 提供进度条功能,向用户显示特定请求传输的数据量。XHR2是新添加的,所以我们需要检查浏览器是否支持它。
以下是检查浏览器是否支持 XHR2 的函数 -
if (Ext.feature.has.XHR2) {
// Here we can write functionality to work if browser supports XHR2
}
我们甚至可以检查浏览器是否支持 XHR2 渐进式上传。
if (Ext.feature.has.XHRUploadProgress) {
// Here we can write functionality to work if browser supports progressive uploads
}
Sencha Touch 中包含各种新的 XHR2 功能。
| 先生编号 | 特点及描述 |
|---|---|
| 1 |
XHR2:正确 这用于启用和禁用应用程序中的 XHR2 功能。 |
| 2 |
扩展字段文件 添加了新的文件字段,以提供有关字段类型的更多信息。 |
| 3 |
外部字段文件输入 这提供了FileInput。 |
| 4 |
外部进度指示器 这是为了提供以进度条形式传输的数据的准确百分比。 |
| 5 |
xtype:文件输入 创建 fileInput 类的实例。 |
| 6 |
xtype:文件字段 创建文件类的实例。 |
| 7 |
响应类型:值 此参数允许各种类型的响应,如文本、文档、blob 等。 |
以下是发送带参数和不带参数的简单ajax请求以及使用ajax上传文件的示例。
不带参数的简单 Ajax 请求 - 成功
<!DOCTYPE html>
<html>
<head>
<link href = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/resources/css/sencha-touch.css" rel = "stylesheet" />
<script type = "text/javascript" src = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/sencha-touch-all.js"></script>
<script type = "text/javascript">
Ext.setup({
requires: [ 'Ext.Panel', 'Ext.Button', 'Ext.form.Panel'], onReady: function() {
var request = {
url: 'https://www.tutorialspoint.com/sencha_touch/index.htm',
method: 'POST',
xhr2: true,
success: function(response) {
Ext.Msg.alert('Ajax call successful');
},
failure: function(response) {
Ext.Msg.alert('Ajax call failed');
}
};
Ext.Viewport.add({
xtype:"panel",
layout:"vbox",
fullscreen:true,
items: [
{
xtype:"button",
text: "Ajax",
ui: 'confirm',
handler: function(){
Ext.Ajax.request(request);
}
}
]
});
}
});
</script>
</head>
<body>
</body>
</html>
它将产生以下结果 -
上面的示例显示 ajax 调用成功,因为提到的 URL 是正确的。在这个例子中,我们没有传递任何参数,它只是一个简单的 ajax 请求,它会点击提到的 URL。
如果您在开发者工具中使用 Chrome 浏览器,导航到网络部分,您可以看到正在发送的请求和我们收到的响应。
不带参数的简单 Ajax 请求 - 失败
<!DOCTYPE html>
<html>
<head>
<link href = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/resources/css/sencha-touch.css" rel = "stylesheet" />
<script type = "text/javascript" src = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/sencha-touch-all.js"></script>
<script type = "text/javascript">
Ext.setup({
requires: [
'Ext.Panel',
'Ext.Button',
'Ext.form.Panel'
],
onReady: function() {
var request = {
url: 'https://www.tutorialspoint.com/sencha_touch/indexx.htm',
method: 'POST',
xhr2: true,
success: function(response) {
Ext.Msg.alert('Ajax call successful');
},
failure: function(response) {
Ext.Msg.alert('Ajax call failed');
}
};
Ext.Viewport.add({
xtype:"panel",
layout:"vbox",
fullscreen:true,
items: [
{
xtype:"button",
text: "Ajax",
ui: 'confirm',
handler: function(){
Ext.Ajax.request(request);
}
}
]
});
}
});
</script>
</head>
<body>
</body>
</html>
它将产生以下结果 -
在上面的例子中,只是为了展示 ajax 失败是如何工作的,我们提到了错误的 URL。比较一下这个和前面的例子,你会发现不同之处。
Ajax请求中发送参数
<!DOCTYPE html>
<html>
<head>
<link href = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/resources/css/sencha-touch.css" rel = "stylesheet" />
<script type = "text/javascript" src = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/sencha-touch-all.js"></script>
<script type = "text/javascript">
Ext.setup({
requires: [
'Ext.Panel',
'Ext.Button',
'Ext.form.Panel'
],
onReady: function() {
var formData = new FormData();
formData.append("firstName", "Hi");
formData.append("lastName", "Reader");
// Request will be sent as part of the payload instead of standard post data
var request = {
url: 'https://www.tutorialspoint.com/sencha_touch/sencha_json.php',
method: 'POST',
xhr2: true,
data: formData,
success: function(response) {
var out = Ext.getCmp("output");
response = Ext.JSON.decode(response.responseText, true);
Ext.Msg.alert(response.message);
},
failure: function(response) {
var out = Ext.getCmp("output");
Ext.Msg.alert('Ajax failed!');
}
};
Ext.Viewport.add({
xtype:"panel",
layout:"vbox",
fullscreen:true,
items: [
{
xtype:"button",
text: "Ajax",
ui: 'confirm',
handler: function(){
Ext.Ajax.request(request);
}
}
]
});
}
});
</script>
</head>
<body>
</body>
</html>
它将产生以下结果 -
在此示例中,我们使用 ajax 调用的 data 属性通过 ajax 传递参数。
使用 Ajax 上传文件
<!DOCTYPE html>
<html>
<head>
<link href = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/resources/css/sencha-touch.css" rel = "stylesheet" />
<script type = "text/javascript" src = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/sencha-touch-all.js"></script>
<script type = "text/javascript">
Ext.setup({
requires: [
'Ext.Panel',
'Ext.MessageBox',
'Ext.Button',
'Ext.ProgressIndicator',
'Ext.form.Panel',
'Ext.field.FileInput'
],
onReady: function() {
var progressIndicator = Ext.create("Ext.ProgressIndicator", {
loadingText: "Uploading: {percent}%"
});
var request = {
url: 'https://www.tutorialspoint.com/sencha_touch/sencha_json.php',
method: 'POST',
xhr2: true,
progress:progressIndicator,
success: function(response) {
Ext.Msg.alert('File uploaded successfully.');
},
failure: function(response) {
Ext.Msg.alert('File upload failed.');
}
};
Ext.Viewport.add(progressIndicator);
Ext.Viewport.add({
xtype:"panel",
layout:"vbox",
fullscreen:true,
items: [
{
xtype:"fileinput",
accept:"image/jpeg"
},
{
xtype:"button",
text: "Upload",
ui: 'confirm',
handler: function(){
var input = Ext.Viewport.down("fileinput").input;
var files = input.dom.files;
if (files.length) {
request.binaryData = files[0];
Ext.Ajax.request(request);
}else {
Ext.Msg.alert("Please Select a JPG");
}
}
}
]
});
}
});
</script>
</head>
<body>
</body>
</html>
它将产生以下结果 -
这个例子展示了如何使用ajax调用上传数据。在此示例中,我们使用进度条指示器来显示上传文件的进度。