0

Hi I'm having trouble with an array I get via ajax post. My return on success looks like this:

["OptimiseWeb","Photos","Portal","Projects","Public"]

My problem is that when I do an alert(this) foreach item in the array instead of getting the directory ie. Photos. It's treating it as a string and is looping through each character.

Here is my code:

$.ajax({
    type: "POST",
    url: "<?php echo base_url(); ?>filesystem/get_dropbox_directories", 
    data: dataString,
    dataType: JSON,
    success: function(arr)
    {
        alert(arr);
        $.each(arr, function() {
            alert(this);
            var opt = $('<option />'); 
            opt.val(this);
            opt.text(this);
            $('#dropbox-directories').append(opt); 

        });


        $('.drop-account-indicator').css('display', 'none');
    }
});

Can anyone see where I'm going wrong? Here is my PHP if it helps?

$data = array();

        foreach($contents as $sub){

            if($sub->is_dir){
                $data[] = str_replace('\/', '', $sub->path);
            }

        }

        header('Content-type: application/json');
        echo json_encode($data);
1
  • btw this is not required: <?php echo base_url(); ?> a relative url should be fine Commented Aug 21, 2011 at 12:04

1 Answer 1

1

JSON should be "json":

dataType: "json",

dataType should be a string. JSON passes a global object, while "json" passes a string (jQuery doesn't know what to do with JSON, but with "json" it will parse the response).

jQuery.ajax docs

Sign up to request clarification or add additional context in comments.

4 Comments

Wow thanks for the quick answer. And it worked thanks! Stupid mistake :(
The JSON should hv thrown a js error and the ajax call shouldnt have gone through.. nt sure if this is his prob.. may be he has a variable names JSON.. @iamjonesy did this fix your problem?
@zzzz JSON is included as an object in some browsers. It's a JSON parsing library.
Didn't get any JS errors. Tried debugging through chrome and FF consoles and no error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.