【问题标题】:VueJs - Axios: How to send multiple value in params?VueJs - Axios:如何在参数中发送多个值?
【发布时间】:2018-11-06 14:29:33
【问题描述】:

你能帮帮我吗? 我有带有 body 参数的 POST 方法,var product_ids 中有两个值

BODY
product_ids = 8|559
customer_type_id = 2
qty_from = 11
sell_price = 10001

这是我的 axios 代码

createNewPricetiers() {
  this.loading = true
  let headers = {
    Authorization: 'Bearer ' + window.accessToken
  }
  let data = {
    product_ids: this.$route.params.id || this.form.variant,
    customer_type_id: this.form.customer,
    qty_from: this.form.qty,
    sell_price: this.form.sellPrice
  }
  axios({
    method: 'POST',
    url: BASE_API + 'productpricetiers',
    headers: headers,
    data: data
  })
  ...

但是当它调用服务器时,多个参数不能运行 只需发送参数this.$route.params.id 我的错在哪里?

【问题讨论】:

  • 你的意思是product_ids: `${this.$route.params.id}|${this.form.variant}`吗? || 是布尔“或”运算符
  • 是的,我知道,但是 product_ids 中正文的后端请求是带分隔符的两个值
  • 这就是我从问题顶部的示例中得出的结论。那么,你尝试过我的建议了吗?如果您不喜欢字符串模板文字,也可以使用 product_ids: this.$route.params.id + '|' + this.form.variant。甚至[this.$route.params.id, this.form.variant].join('|')。结果是一样的
  • 投票结束是一个错字。您应该创建一个以竖线分隔的字符串,而不是布尔表达式
  • 太棒了,谢谢@phil。但是您都建议它是字符串值而不是整数?

标签: vuejs2 axios


【解决方案1】:

使用 axios.post 时添加默认标题:

import axios from 'axios';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

【讨论】:

  • 是什么让你认为 OP 不想要 JSON?无论如何,除了设置Content-Type 标头之外,还有更多工作要做。见github.com/axios/…
  • 我不明白,你能举个例子吗
  • @dede.brahma 如果您的服务器端端点接受 JSON,您可以完全忽略此答案
猜你喜欢
  • 2018-02-15
  • 1970-01-01
  • 2019-06-27
  • 1970-01-01
  • 2020-05-12
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
相关资源
最近更新 更多