开发者

Serialize json 2-dimensional array, send two arrays, or other?

开发者 https://www.devze.com 2023-03-21 22:49 出处:网络
I have to send a 2-dimensional array (along with several other variables) to PHP using jQuery.ajax().I believe my options are:

I have to send a 2-dimensional array (along with several other variables) to PHP using jQuery.ajax(). I believe my options are:

  1. Serialize to json w开发者_运维技巧ith JSON-js
  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消