ajax call with this data
variable gives expected POST data in the controller(server)
Javascript:
var data = "item[]=9&item[]=1&item[]=2&item[]=3&item[]=4&item[]=5&item[]=6&item[]=7&item[]=8"
$.ajax({
data: data,
type: 'POST',
url: '/api/call'
});
Controller:
array:1 [
"item" => array:9 [
0 => "1"
1 => "2"
2 => "4"
3 => "3"
4 => "5"
5 => "6"
6 => "7"
7 => "8"
8 => "9"
]
]
in my requirement, I create a new variable
Javascript:
var data = "item[]=9&item[]=1&item[]=2&item[]=3&item[]=4&item[]=5&item[]=6&item[]=7&item[]=8"
var = other_parameters => [
"host" => "host name"
"session" => "current session"
"timestamp" => "time stamp"
]
let requestData = {
other_parameters,
data
}
$.ajax({
data: requestData,
type: 'POST',
url: '/api/call'
});
Controller(Currently I get this data in the controller):
array:2 [
"other_parameters" => array:3 [
"host" => "host name"
"session" => "current session"
"timestamp" => "time stamp"
]
"data" => "item[]=1&item[]=2&item[]=3&item[]=5&item[]=4&item[]=6&item[]=7&item[]=8&item[]=9"
]
Expected:
array:2 [
"other_parameters" => array:3 [
"host" => "host name"
"session" => "current session"
"timestamp" => "time stamp"
],
"data" => [
"item" => array:9 [
0 => "1"
1 => "2"
2 => "4"
3 => "3"
4 => "5"
5 => "6"
6 => "7"
7 => "8"
8 => "9"
]
]
]
Please help to get expected data in Controller(Server)
data
string in the same format. Either that or convert the whole thing to an object structure in JS and pass that object to jQuery. What's happening now is a hybrid system which doesn't work: you're puttingdata
into the object as a string. jQuery then form-url-encodes your whole object before sending it in the request. It doesn't take account thatdata
is already an encoded string, it just treats it as a plain text property of the object, and thus so does PHP. – ADyson