news 2026/8/6 18:35:36

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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" } ] } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/6 18:34:32

AI通信协议MCP的安全隐患与加固实践

1. 项目概述&#xff1a;当AI生态遇上"通用接口"的暗流去年参与某跨国AI平台安全审计时&#xff0c;我在协议分析仪上发现一组异常数据包——它们完美遵循MCP协议规范&#xff0c;却在payload里藏匿着精心构造的恶意指令。这就像USB-C接口的充电线里暗藏数据窃取芯片…

作者头像 李华
网站建设 2026/8/6 18:32:58

在银行营业厅:接待机器人正在覆盖“排队等“背后的标准化问询

近年来&#xff0c;金融机构的线下展厅、营业厅和体验中心正在经历一场深刻的智能化变革。以中信证券在世界人工智能大会上展示的“超级研究员”AI数字员工、招商银行打造的450平方米沉浸式数字金融展厅为代表的行业实践表明&#xff0c;智能化升级已从单纯的“硬件展示”阶段&…

作者头像 李华
网站建设 2026/8/6 18:31:34

3分钟掌握Python盲水印技术:保护你的数字图像版权

3分钟掌握Python盲水印技术&#xff1a;保护你的数字图像版权 【免费下载链接】BlindWaterMark 盲水印 by python 项目地址: https://gitcode.com/gh_mirrors/bli/BlindWaterMark 在数字时代&#xff0c;图像版权保护成为了创作者面临的重要挑战。你是否曾担心自己的作品…

作者头像 李华
网站建设 2026/8/6 18:31:03

Audacity终极音频编辑指南:如何用免费开源软件打造专业音质

Audacity终极音频编辑指南&#xff1a;如何用免费开源软件打造专业音质 【免费下载链接】audacity Audio Editor 项目地址: https://gitcode.com/GitHub_Trending/au/audacity Audacity是一款功能强大的免费音频编辑软件&#xff0c;支持多轨录音和音频处理&#xff0c…

作者头像 李华