这篇笔记主要记录,uni-app小程序,利用表单增加数据, 在线上或真机预览的时候提示,request: fail url not in domain list
,
原因就是: 请求的服务器地址没有在微信小程序后台的合法域名列表中进行配置。
左侧是新增数据的时候提示,request: fail url not in domain list
,右侧是问题解决提示新增成功
我用的是unicloud阿里云,这个是提交的表单代码,是用schema2code
生成的
const db = uniCloud.database();
const dbCollectionName = 'chore';
/**
* 提交表单
*/
submitForm(value) {
// 使用 clientDB 提交数据
return db.collection(dbCollectionName).add(value).then((res) => {
uni.showToast({
icon: 'none',
title: '新增成功'
})
this.getOpenerEventChannel().emit('refreshData')
setTimeout(() => uni.navigateBack(), 500)
}).catch((err) => {
uni.showModal({
content: err.message || '请求服务失败',
showCancel: false
})
})
}
完整代码, 根据配置的collectionName.schema.json
生成的
<template>
<view class="uni-container">
<uni-forms ref="form" :model="formData" validate-trigger="submit" err-show-type="toast">
<uni-forms-item name="time" label="干活的时间" required>
<uni-easyinput v-model="formData.time"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="description" label="干了啥" required>
<uni-easyinput v-model="formData.description"></uni-easyinput>
</uni-forms-item>
<view class="uni-button-group">
<button type="primary" class="uni-button" @click="submit">提交</button>
</view>
</uni-forms>
</view>
</template>
<script>
import { validator } from '../../js_sdk/validator/chore.js';
const db = uniCloud.database();
const dbCollectionName = 'chore';
function getValidator(fields) {
let result = {}
for (let key in validator) {
if (fields.indexOf(key) > -1) {
result[key] = validator[key]
}
}
return result
}
export default {
data() {
let formData = {
"time": "",
"description": ""
}
return {
formData,
formOptions: {},
rules: {
...getValidator(Object.keys(formData))
}
}
},
onReady() {
this.$refs.form.setRules(this.rules)
},
methods: {
/**
* 验证表单并提交
*/
submit() {
uni.showLoading({
mask: true
})
this.$refs.form.validate().then((res) => {
return this.submitForm(res)
}).catch(() => {
}).finally(() => {
uni.hideLoading()
})
},
/**
* 提交表单
*/
submitForm(value) {
// 使用 clientDB 提交数据
return db.collection(dbCollectionName).add(value).then((res) => {
uni.showToast({
icon: 'none',
title: '新增成功'
})
this.getOpenerEventChannel().emit('refreshData')
setTimeout(() => uni.navigateBack(), 500)
}).catch((err) => {
uni.showModal({
content: err.message || '请求服务失败',
showCancel: false
})
})
}
}
}
</script>
<style>
.uni-container {
padding: 15px;
}
.uni-input-border,
.uni-textarea-border {
width: 100%;
font-size: 14px;
color: #666;
border: 1px #e5e5e5 solid;
border-radius: 5px;
box-sizing: border-box;
}
.uni-input-border {
padding: 0 10px;
height: 35px;
}
.uni-textarea-border {
padding: 10px;
height: 80px;
}
.uni-button-group {
margin-top: 50px;
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
justify-content: center;
}
.uni-button {
width: 184px;
}
</style>
解决方法
- 登录 微信公众平台。
- 进入 管理 -> 开发管理 页面。
- 找到 服务器域名 部分,点击 修改。
- 在以下三个域名配置中添加相应的域名:
• request 合法域名(HTTP 请求)
• uploadFile 合法域名(文件上传)
• downloadFile 合法域名(文件下载)
前面说过了,我用的是阿里云,进入unicloud控制台,就可以看到,内置云存储上传域名
, 内置云存储下载域名
, request域名
然后再重启下小程序就可以啦~