0
votes

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)

1
How exactly does your API call endpoint return the data? Are you just rendering a string there?pdu
You need to append the other values to the 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 putting data 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 that data is already an encoded string, it just treats it as a plain text property of the object, and thus so does PHP.ADyson
What have you tried so far? Where are you stuck? Is this really a PHP problem, or a pure JS problem?Nico Haase
I rolled back your recent edit. Don't change your question to a fundamentally different one after it's already been answered and commented on - it makes the existing contributions from other people make less/no sense, which is unhelpful for future readers.ADyson

1 Answers

2
votes

You could try and use parse_str, REF: https://www.php.net/manual/en/function.parse-str.php

Example:

$str = 'item[]=1&item[]=2&item[]=3&item[]=5&item[]=4&item[]=6&item[]=7&item[]=8&item[]=9'
parse_str($str, $output);
var_dump($output);

// Output
array:1 [▼
  "item" => array:9 [▼
    0 => "1"
    1 => "2"
    2 => "3"
    3 => "5"
    4 => "4"
    5 => "6"
    6 => "7"
    7 => "8"
    8 => "9"
  ]
]

It should get you to where you need to be.

Edit:

A better way would probably be to convert all your data into a JSON array first and send that. Then just use json_decode to get your array in PHP.