Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] Something's not right about my JSON



On Sat, Aug 14, 2010 at 8:11 PM, Dave M G <dave@example.com> wrote:
>
> I've tried creating this JSON sting using the following code (edited a
> bit for brevity - hopefully no typos/syntax errors):
>
> var loginData=new Array();

I think you at least want a "new Object();", not "new Array():"
An Object should be used for something containing named values,
whereas an Array is for a plain list of items.

> loginData.email=document.getElementById('email').value;
> loginData.password=$.md5(document.getElementById('password').value);
> userData=new Array();

here too, s/Array/Object/

> userData.login=loginData;
> userData.newUserData="No";
> jsonString = JSON.stringify(userData);
> $.post('jsonhandler.php', {JSON: jsonString}, checkResult, "json");
>

But JSON is native to javascript so literal syntax is usually easier.
You could just write something like:

var userData = {
    "loginData" : {
      "email" : document.getElementById('email').value,
      "password" : $.md5(document.getElementById('password').value),
    },
    "newUserData" : "No",
};

$.post(
    'jsonhandler.php',
    {JSON: JSON.stringify(userData)},
    checkResult,
    "json");


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links