I have to send a 2-dimensional array (along with several other variables) to PHP using jQuery.ajax()
. I believe my options are:
- Serialize to json w开发者_运维技巧ith JSON-js
- Send both arrays as csv strings and recompile on the other side.
My questions are:
A. Is there anything wrong with option #2 if I'd prefer not to include another library for a small function? B. Are there other options besides options #1 and #2?
Thanks!
You can try :
JSON.stringify(array);
No extra library required.
I'm sure there are other options; there's always a million ways to skin a cat. But I'd suggest option #1. When you send it as a JSON string (using a library that is quite small and only has two methods), you can then decode it in PHP using json_decode. No fuss, no muss.
A. Is there anything wrong with option #2 if I'd prefer not to include another library for a small function?
You might have issues with escaping/unescaping some characters.
B. Are there other options besides options #1 and #2?
Surely there must be. But I'd go with JSON, it's the simplest and cleanest solution.
精彩评论