Welcome Guest, Not a member yet? Register   Sign In
Handling returned JSON data
#1

[eluser]dettie[/eluser]
Hi,

In javascript I have the following:

Code:
afterEachUpload: function(data) {
    $("#content").append(data);
   }

Which outputs:

Code:
{"file_name":"filename.jpg","file_type":"image\/jpeg","file_path":"\/var\/www\/website\/uploads\/","full_path":"\/var\/www\/website\/uploads\/filename.jpg","raw_name":"filename","orig_name":"filename.jpg","client_name":"filename.jpg","file_ext":".jpg","file_size":46.61,"is_image":true,"image_width":438,"image_height":215,"image_type":"jpeg","image_size_str":"width=\"438\" height=\"215\""}

How do I output or access a specific value? For example the "file_name" key only ...
I've already tried things such as data['file_name'] and data[0]['file_name'] etc which does not work..
#2

[eluser]CroNiX[/eluser]
$(data).file_name

Its not an array, its a json object.
#3

[eluser]dettie[/eluser]
[quote author="CroNiX" date="1327378610"]$(data).file_name

Its not an array, its a json object.[/quote]
that returns "undefined"
#4

[eluser]CroNiX[/eluser]
Strange, works here using jQuery.
#5

[eluser]CroNiX[/eluser]
Where are you trying to access it...in jQuery or in php? If its php you need to run json_decode() to turn it back into a php array.
#6

[eluser]dettie[/eluser]
in jquery/javascript..

The controller returns the data like so (if that matters):
Code:
echo json_encode($data);

and I tried what you said like so;
Code:
afterEachUpload: function(data) {
    $("#content").append($(data).file_name);
   }
#7

[eluser]CroNiX[/eluser]
Maybe try
Code:
$("#content").append(data.file_name);
#8

[eluser]dettie[/eluser]
nope same thing ... I'm starting to think Codeigniter and Ajax don't really mix that well -_-
#9

[eluser]dettie[/eluser]
can perhaps anyone recommend an ajax upload that works flawless with CI?
#10

[eluser]CroNiX[/eluser]
When you output the json, did you output json headers?




Theme © iAndrew 2016 - Forum software by © MyBB