在axios请求中将数组作为参数传递 [英] pass array as parameter in an axios request

查看:45
本文介绍了在axios请求中将数组作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 axios 发出请求,在该请求中,我希望将 [1,2,3,4] 类型的数组作为数组传递.我需要这些数据来从后端进行选择查询,我的问题是:我应该使用 GET 还是 POST 请求,以及传递此数组的正确方法是什么??

I need to make a request through axios, in which I want to pass as an array an array of this type [1,2,3,4]. I need this data to make a selection query from my backend, my question is: should I use a GET or POST request and what would be the correct way to pass this array?

推荐答案

您可以 POST 作为json数据

You can POST it as json data

let data=[1,2,3,4,5];
let json=JSON.stringify(data);
let post_data={json_data:json}
axios.post('/url',post_data)

  • 使用 JSON.stringify 将其转换为json字符串
  • 使用POST方法将数据发送到服务器
  • 使用json_decode将json转换回服务器端的数组
    • use JSON.stringify to convert it to json string
    • Use POST method to send data to server
    • Use json_decode to convert json back to array on server side
    • 在laravel方面,您可以像下面这样

      On laravel side you can do like below

      $jsonArray = json_decode($response,true);
      

      这篇关于在axios请求中将数组作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆