tags:

views:

183

answers:

4

I'm having a killer time trying to create a JSON object to return to a jQuery request.

I'm trying to use the jQuery chained select module

and I'm trying to create the JSDN object using Perl's JSON module.

I have no idea what I'm doing wrong or how I can even debug it, about the best I can do is get a JS dialog box coming up with "A unknown error....".

This is all I was trying to do to create the JSON object, I thought this would be all that is needed, but it's not. Any ideas?

TIA

use JSON;
my %data = (1 => 'val1',
            2 => 'val2',
            3 => 'val3',
           );
my $json_text = to_json(\%data);
print $json_text;
+3  A: 

Running the script gives:

{"1":"val1","3":"val3","2":"val2"}

… which is the expected output.

The problem almost certainly lies with whatever code you are using to get the JSON from your server to the client.

David Dorward
I'm just printing it out - I thought that was all that was needed? "print $json_text"
Chris
@Chris: The code snippet you provided in the question above is correct, and works properly. Try it again yourself in a standalone script and see. (So it must be something else in your script that is wrong.)
Ether
Where are you printing it to? What does the output look like?
David Dorward
+1  A: 

Have the javascript output the response text before processing to a textarea so you can see if there's something failing in transport. Also, you're going to want to put something in front of the data, like "myVar= " so that the data is assigned to something on the other end.

woolstar
Thanks Woolstar, I'll try to figure out how to do that (I know nothing about JQuery)
Chris
A: 

Thanks all for the help - I guess the problem was simply the JSON module, it wasn't creating the OBJ for some reason. I tried JSON::Any and it work fine.

Thanks Again!

Chris
A: 

Looks like you already figured it out your issue, and if you were wondering why it wasn't working is because the to_json method is not exported from JSON.pm in the older versions of the JSON module. The latest version on CPAN (2.0) exports to_json into your namespace but versions ~1.5 don't and require the JSON::. Many repos still carry this older version, and if you are running on CentOS ~5.3 or an older version of Fedora Core you will have the older JSON.pm.

dwp