当前位置: 首页 > news >正文

amis调用接口的几种常见方式

一、service

amis - 低代码前端框架-servicehttps://aisuda.bce.baidu.com/amis/zh-CN/components/service

使用场景:

1.需要在form组件中单独处理保存接口

2.需要存储变量,给页面使用

{ "type": "button", "label": "新增", "actionType": "dialog", "dialog": { "body": { "type": "form", "debug": true, "api": { "url": "/amis/api/mock2/form/saveForm", "method": "POST", "data": { "query_no": "${query_no-0+1}" } }, "body": [ { "id": "service-1", "type": "service", "api": { "url": "/amis/api/mock2/options/chainedOptions?waitSeconds=1&parentId=$parentId&level=$level&maxLevel=4", "method": "GET", "responseData": { "options": "${items|pick:index,value,label}" } }, "initFetch": false, "body": { "name": "query_no", "type": "hidden", "label": "前一个编号", "required": "true", "value": "${options[options.length-1].index}" } } ] }, "actions": [ { "type": "button", "label": "保存", "onEvent": { "click": { "actions": [ { "actionType": "reload", "componentId": "service-1" } ] } } } ] } }

二、ajax

amis - 低代码前端框架-发送-http-请求https://aisuda.bce.baidu.com/amis/zh-CN/docs/concepts/event-action#%E5%8F%91%E9%80%81-http-%E8%AF%B7%E6%B1%82

amis - 低代码前端框架-Action行为按钮https://aisuda.bce.baidu.com/amis/zh-CN/components/action#ajax-%E8%AF%B7%E6%B1%82

使用场景:不需要校验输入框错误信息就直接调用接口的场景,比如期望保存接口和提交接口分开处理

代码示例:

{ "type": "page", "data": { "name": "lll", "id": "222" }, "body": [ { "type": "button", "id": "b_001", "label": "发送 Ajax 请求", "level": "primary", "confirmText": "确认要发出这个请求?", "onEvent": { "click": { "actions": [ { "actionType": "ajax", "args": { "api": { "url": "/amis/api/mock2/form/saveForm?name=${name}", "method": "post", "data": { "resId": "${id}" }, "messages": { "success": "成功了!欧耶", "failed": "失败了呢。。" } } } } ] } } } ] }

简化1:

{ "type": "page", "data": { "name": "lll", "id": "222" }, "body": [ { "type": "button", "id": "b_001", "label": "发送 Ajax 请求", "level": "primary", "confirmText": "确认要发出这个请求?", "onEvent": { "click": { "actions": [ { "actionType": "ajax", "api": { "url": "/amis/api/mock2/form/saveForm?name=${name}", "method": "post", "data": { "resId": "${id}" }, "messages": { "success": "成功了!欧耶", "failed": "失败了呢。。" } } } ] } } } ] }

简写2:

{ "type": "page", "data": { "name": "lll", "id": "222" }, "body": [ { "type": "button", "id": "b_001", "label": "发送 Ajax 请求", "level": "primary", "confirmText": "确认要发出这个请求?", "actionType": "ajax", "api": { "url": "/amis/api/mock2/form/saveForm?name=${name}", "method": "post", "data": { "resId": "${id}" }, "messages": { "success": "成功了!欧耶", "failed": "失败了呢。。" } } } } ] }

三、custom + XMLHttpRequest

amis - 低代码前端框架-自定义jshttps://aisuda.bce.baidu.com/amis/zh-CN/docs/concepts/event-action#%E8%87%AA%E5%AE%9A%E4%B9%89-jsXML HttpRequesthttps://www.w3school.com.cn/xml/xml_http.asp原生JavaScript的xhr代码示例:

get请求示例:

var xhr = new XMLHttpRequest(); var params = new URLSearchParams(); params.append('a', 1); params.append('b', 1); xhr.open('GET', '/xxx?' + params.toString(), true); xhr.onreadystatechange = function() { if (xhr.readyState === 4) { if (xhr.status === 200) { console.log(xhr.responseText); } else { console.error('请求失败:', xhr.status); } } }; xhr.send();

post请求示例:

var xhr = new XMLHttpRequest(); xhr.open('POST', '/xxx', true); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onreadystatechange = function() { if (xhr.readyState === 4) { if (xhr.status === 200) { console.log(xhr.responseText); } else { console.error('请求失败:', xhr.status); } } }; xhr.send(JSON.stringify({ a: 1, b: 1 }));

amis框架代码示例:

{ "type": "page", "data": { "name": "lll", "id": "222" }, "body": [ { "type": "button", "id": "b_001", "label": "发送 Ajax 请求", "level": "primary", "confirmText": "确认要发出这个请求?", "onEvent": { "click": { "actions": [ { "actionType": "custom", "script": "const {name, id} = event.data;\n\nvar xhr = new XMLHttpRequest();\nxhr.open('POST', '/amis/api/mock2/form/saveForm?name=' + encodeURIComponent(name), true);\nxhr.setRequestHeader('Content-Type', 'application/json');\n\nxhr.onload = function() {\n if (xhr.status >= 200 && xhr.status < 300) {\n var result = JSON.parse(xhr.responseText);\n event.setData({ resId: result.id || id });\n\n doAction({\n actionType: 'toast',\n args: {\n msgType: 'info',\n msg: '成功了!欧耶'\n }\n });\n } else {\n doAction({\n actionType: 'toast',\n args: {\n msgType: 'error',\n msg: '失败了呢。。'\n }\n });\n }\n};\n\nxhr.onerror = function() {\n doAction({\n actionType: 'toast',\n args: {\n msgType: 'error',\n msg: '失败了呢。。'\n }\n });\n};\n\nxhr.send(JSON.stringify({ resId: id }));" } ] } } } ] }

四、form组件,使用api属性

{ "type": "button", "label": "新增", "actionType": "dialog", "dialog": { "body": { "type": "form", "debug": true, "api": { "url": "/amis/api/mock2/form/saveForm", "method": "POST", "data": { "query_no": "${query_no-0+1}" } }, "body": [ { "type": "service", "api": { "url": "/amis/api/mock2/options/chainedOptions?waitSeconds=1&parentId=$parentId&level=$level&maxLevel=4", "method": "GET", "responseData": { "options": "${items|pick:index,value,label}" } }, "body": { "name": "query_no", "type": "hidden", "label": "前一个编号", "required": "true", "value": "${options[options.length-1].index}" } } ] }, "actions": [ { "type": "button", "label": "保存", "actionType": "submit" } ] } }
http://www.jsqmd.com/news/1341328/

相关文章:

  • 2026 国赛倒计时 36 天:选题决定上限,这 5 条铁律帮你避开 90% 的坑
  • 如何使用PushPin打造高效协作空间:新手入门的完整指南
  • 2026靠谱的青岛化妆培训推荐 美业学习实用指南 - 谁都没有我好看
  • 消防水箱品牌盘点:弗科斯凭这些“硬核实力”领跑行业?四大厂商核心竞争力大揭秘 - 兔兔不是荼荼
  • Work Buddy 深夜暴走:用户消息覆盖系统提示后,我的分层测试竟集体失效
  • Handshake全节点完整指南:从零开始掌握HSD终极教程
  • Vue3 浏览器打印实践:print-js + 服务端条码
  • AI人工智能培训机构哪家好?全方位行业解析与机构深度测评 - IT培训品牌推荐
  • 终极指南:如何免费为PotPlayer配置百度字幕实时翻译插件
  • AF 546 SNA 不同缓冲体系下生物活性变化测试
  • 2026年8月济南本地推开户政策和投放技巧 - 产品评测官
  • Windows安卓应用安装指南:告别笨重模拟器的终极解决方案
  • 终极艺术家数据挖掘:Pyechonest Artist模块10大核心功能详解
  • 打破Windows崩溃分析壁垒:用AI对话式调试彻底改变你的调试体验
  • 2026年刑事案件家属首次咨询,看会见与取保评估 - 科技焦点
  • 独立站SEO怎么做才能出单?
  • FanControl终极指南:Windows电脑风扇控制的免费高效解决方案
  • 骑行拍摄智能眼镜主控:JL681N单芯片方案测评 - 生活动态圈
  • 大模型训练数据泄露事件频发(内部白皮书级复盘):从特斯拉到OpenAI的6起未公开安全事故溯源
  • 3分钟快速上手:CVPR 2024顶级6D姿态估计算法FoundationPose完整教程
  • 企业 AI Agent 上线前,为什么必须先做评估体系?
  • Windows窗口置顶工具AlwaysOnTop:如何让关键信息始终在最前端?
  • 2026北京外墙漏水维修怎么处理 窗台渗水窗户漏水原因解析 - 科技先行者
  • 支付宝前端团队集体转向Agent开发,小白程序员抓住下一个技术风口!
  • 群晖NAS的Realtek USB网卡驱动:突破千兆网络瓶颈的技术实现
  • 惠州亲密关系疏导哪家好:【谋仕心理】拥有松弛心态 - 17328623207
  • 合肥共达职业技术学院预科班报考注意事项(招生简章发布) - 教育为先
  • 计算机考研408高效复习指南:掌握四大专业课的核心资源与实战技巧
  • Fast-DDS:为什么它是构建实时分布式系统的首选中间件?
  • 沧州钢铝拖链实力厂家盘点:桥式/全封闭款全覆盖,适配加工中心、激光机场景 - 品牌推荐大师