我如何 post 数据到 json-server 在 vuejs 中正确?
How do I post data to json-server properly in vuejs?
使用原始值将数据发布到 json-服务器 posts:
{
"title": {
"__v_isShallow": false,
"dep": {
"w": 0,
"n": 0
},
"__v_isRef": true,
"_rawValue": "What is Vuex?",
"_value": "What is Vuex?"
},
"body": {
"__v_isShallow": false,
"dep": {
"w": 0,
"n": 0
}
}
我怎样才能像这样达到 post:
{
"id": 1,
"title": "Introduction to JavaScript array filter() method",
"body": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolor, beatae! Alias, possimus asperiores repudiandae ipsam, molestiae doloremque animi magni impedit optio commodi similique consectetur ullam, quaerat nam. Sint, porro dicta? Libero ab officiis explicabo architecto dolorum consequatur eius? Soluta voluptas deleniti corrupti sed pariatur exercitationem error. Veritatis, illo deserunt. Error, dolore inventore doloribus sapiente rerum quas quidem id in. Quisquam mollitia consequuntur animi nostrum. Provident et molestias eaque eligendi illo."
}
import axios from 'axios'
import { ref } from 'vue'
const addPost = (title, body, tagObj) => {
const err = ref(null)
const json = JSON.stringify({ title: title });
const post = async () => {
try {
const res = await axios.post('localhost:3000/posts', { title: title, body: body, tags: tagObj })
} catch(error) {
err.value = "There was a problem adding post..."
}
}
return { post, err }
}
export default addPost
看起来 title
和 body
是 ref
,您正在将其直接传递给服务器。
解决方案是用 .value
展开 ref
的值:
const addPost = (title, body, tagObj) => {
const res = await axios.post('localhost:3000/posts', { title: title.value, body: body.value, tags: tagObj })
}
使用原始值将数据发布到 json-服务器 posts:
{
"title": {
"__v_isShallow": false,
"dep": {
"w": 0,
"n": 0
},
"__v_isRef": true,
"_rawValue": "What is Vuex?",
"_value": "What is Vuex?"
},
"body": {
"__v_isShallow": false,
"dep": {
"w": 0,
"n": 0
}
}
我怎样才能像这样达到 post:
{
"id": 1,
"title": "Introduction to JavaScript array filter() method",
"body": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolor, beatae! Alias, possimus asperiores repudiandae ipsam, molestiae doloremque animi magni impedit optio commodi similique consectetur ullam, quaerat nam. Sint, porro dicta? Libero ab officiis explicabo architecto dolorum consequatur eius? Soluta voluptas deleniti corrupti sed pariatur exercitationem error. Veritatis, illo deserunt. Error, dolore inventore doloribus sapiente rerum quas quidem id in. Quisquam mollitia consequuntur animi nostrum. Provident et molestias eaque eligendi illo."
}
import axios from 'axios'
import { ref } from 'vue'
const addPost = (title, body, tagObj) => {
const err = ref(null)
const json = JSON.stringify({ title: title });
const post = async () => {
try {
const res = await axios.post('localhost:3000/posts', { title: title, body: body, tags: tagObj })
} catch(error) {
err.value = "There was a problem adding post..."
}
}
return { post, err }
}
export default addPost
看起来 title
和 body
是 ref
,您正在将其直接传递给服务器。
解决方案是用 .value
展开 ref
的值:
const addPost = (title, body, tagObj) => {
const res = await axios.post('localhost:3000/posts', { title: title.value, body: body.value, tags: tagObj })
}