Javascript Axios post待决状态
这是一个在Vue内部使用Axios的简单Post请求:Javascript Axios post待决状态,javascript,vue.js,axios,Javascript,Vue.js,Axios,这是一个在Vue内部使用Axios的简单Post请求: import axios from 'axios' export default { name: 'HelloWorld', props: { msg: String }, mounted () { const code = 'test' const url = 'http://localhost:3456/' axios.post(url, c
import axios from 'axios'
export default {
name: 'HelloWorld',
props: {
msg: String
},
mounted () {
const code = 'test'
const url = 'http://localhost:3456/'
axios.post(url, code, { headers: {'Content-type': 'application/x-www-form-urlencoded', } }).then(this.successHandler).catch(this.errorHandler)
},
methods: {
successHandler (res) {
console.log(res.data)
},
errorHandler (error) {
console.log(error)
}
}
}
Get方法工作得很好。但在“网络”选项卡上以“待定”状态发布停留。我可以确认我的webservice上有一个Post方法,它返回了一些东西(在Postman上测试)
更新
server.post('/', (req, res, next) => {
const { code } = req.params
const options = {
validate: 'soft',
cheerio: {},
juice: {},
beautify: {},
elements: []
}
heml(code, options).then(
({ html, metadata, errors }) => {
res.send({metadata, html, errors})
next()
})
})
以参数形式发送代码:
axios(url, {
method: 'POST',
headers: {
'content-type': 'application/json',
},
params: {
code : 'test'
},
}).then(this.successHandler).catch(this.errorHandler)
WEBSERVICE
server.post('/', (req, res, next) => {
const { code } = req.params
const options = {
validate: 'soft',
cheerio: {},
juice: {},
beautify: {},
elements: []
}
heml(code, options).then(
({ html, metadata, errors }) => {
res.send({metadata, html, errors})
next()
})
})
我认为您的axios请求结构存在问题。 试试这个:
const URL = *YOUR_URL*;
axios(URL, {
method: 'POST',
headers: {
'content-type': 'application/json',
},
data: *YOUR_PAYLOAD*,
})
.then(response => response.data)
.catch(error => {
throw error;
});
如果要发送查询参数:
axios(URL, {
method: 'POST',
headers: {
'content-type': 'application/json',
},
params: {
code: 'your_string'
},
})
如果是path变量,则可以设置url:
const url = `http://localhost:3456/${code}`
如果问题仍然存在,请告诉我现在发生了什么。但这是一个错误:代码:“InvalidContent”,消息:“invalidJSON:位置1处JSON中的意外标记e”。我的Web服务需要一个名为code的参数,它必须是字符串。我建议使用application/json内容类型。您应该发送{code:'your_string'},在服务器端您将得到一个JSON,您可以从请求负载中检索'code'键。您的post请求问题已修复,这是一个解析问题。好的,我会试试这个。还有一个问题:我在localhost:3456上运行webservice,在localhost:8080上运行vue应用程序。这是个问题?找到问题了。我的Web服务期望代码作为参数。已修复!在webservice上使用
const{code}=req.params
,我改为const{code}=req.body