我想在angularjs中发送多维数组。请求从angularjs UI发送到Laravel API端,在API端我需要输入为
array:3 [
0 => array:3 [
"title" => "name1"
"type" => "type1"
"rate" => 11
]
1 => array:3 [
"title" => "name2"
"type" => "type2"
"rate" => 31
]
2 => array:3 [
"title" => "name3"
"type" => "type3"
"rate" => 13.333
]
]
但我没有这样做。我试图从角度发送的静态数据是
var values = [
{
"title" => "name1"
"type" => "type1"
"rate" => 11
},
{
"title": "Make an offer Rate",
"type": "make_offer_rate",
"rate": 30
},
{
"title": "Test price",
"type": "test_rate",
"rate": 100
}
];
$http({
method: 'POST',
url: urls.api_url_serv+'rate?token='+token,
transformRequest: transformRequestAsFormPost,
data: {
rates:JSON.stringify(values),
},
withCredentials: false,
headers: {
'Accept': 'application/json',
'Content-Type': "application/x-www-form-urlencoded"
};
它在API端获取字符串当我尝试dd()时,响应为"[{"title":"name1","type":"type1","rate":45},{"title":"name2","type":"type2","rate":30},{"title":"name3","type":"type3","rate":100}]"
我需要从UI端处理它,因为API部分不是由我完成的
答案 0 :(得分:0)
你的js中有一个对象数组。你需要将它转换为多维数组。
var values = [{
"title" => "name1"
"type" => "type1"
"rate" => 11
},
{
"title": "Make an offer Rate",
"type": "make_offer_rate",
"rate": 30
},
{
"title": "Test price",
"type": "test_rate",
"rate": 100
}
];
var outputData = [];
for (var i = 0; i < values.length; i++) {
var input = inputData[i];
outputData.push([input.Earning, input.Number]);
}
$http({
method: 'POST',
url: urls.api_url_serv + 'rate?token=' + token,
transformRequest: transformRequestAsFormPost,
data: {
rates: JSON.stringify(outputData),
},
withCredentials: false,
headers: {
'Accept': 'application/json',
'Content-Type': "application/x-www-form-urlencoded"
}
});