简体   繁体   中英

jQuery $.post PHP whats the best way to handle a multidimensional object or array

Ok, I usually do this in reverse, post a specific data set, then expect an JSON object back, however. In today's case I kinda need to the opposite. Except my confusion is brought on by there is no static elements to work with. My data won't be coming from a customary form, so serialization is not an option I don't think. So before I go more into it a little info on the spec

I have a Unordered list that can have anywhere from 1 List Element to dozens, each one contains 3 pieces if info I need to pass in a POST to my PHP as I would a form. But Im not sure whats the best way to handle it.

Can I pass it as an array through the post where the array is what it is? Or do I have to transform the output to resemble a JSON object and post that, treating it as a JSON object with the PHP and running it through json_decode? Not sure what to do here, so any advice is greatly appreciated.

edit worth mentioning is I can get the data I need from the list elements, its just what should I do with it to pass it in a post, so that it posts in sense like its a multidimensional object or array

You can pass the array from js using ajax as array. And it will come to $_POST as array without any additional steps

You need to transform it into a JSON string and then json_decode it on the other side. This is where node.js can be superior as a backend because you can pass the array directly to Node without going into and out of JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM