laravel 在 axios 请求中将数组作为参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49682409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:36:25  来源:igfitidea点击:

pass array as parameter in an axios request

javascriptajaxlaravelaxios

提问by FeRcHo

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 GETor POSTrequest and what would be the correct way to pass this array?

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

回答by sumit

You can POSTit as json data

您可以将POST其作为 json 数据

let data=[1,2,3,4,5];
let json=JSON.stringify(data);
let post_data={json_data:json}
axios.post('/url',post_data)
  • use JSON.stringifyto 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
  • 用于 JSON.stringify将其转换为 json 字符串
  • 使用POST方法向服务器发送数据
  • 在服务器端使用 json_decode 将 json 转换回数组

On laravel side you can do like below

在 Laravel 方面,您可以像下面这样

$jsonArray = json_decode($response,true);