2

I'm creating an image uploader using Codeigniter3 and jQuery / Ajax.

Issue: I don't understand how to find the index of the array that I get from the ajax response as below result.

Here is the result when images upload

{
    "res": true,
    "img_pro": {
        "upload": {
            "file_name": "web_page-_500_x_420px-06s1.png",
            "file_type": "image\/png",
            "file_path": "/uploads\/",
            "full_path": "/uploads\/web_page-_500_x_420px-06s1.png",
            "raw_name": "web_page-_500_x_420px-06s1",
            "orig_name": "web_page-_500_x_420px-06s.png",
            "client_name": "web_page-_500_x_420px-06s.png",
            "file_ext": ".png",
            "file_size": 233.79,
            "is_image": true,
            "image_width": 563,
            "image_height": 420,
            "image_type": "png",
            "image_size_str": "width=\"563\" height=\"420\""
        }
    },
    "token": "e291d9b7176d23647470083f7ccfd166"
}

And I used $.ajax for sending images to the server

<script>
    $(document).ready(function () {
        $("#submit").on('click', function () {

            $.ajax({
                url: 'http://localhost/cootel/gotoadmin/image/upload',
                type: 'POST',
                data: new FormData($('#img_upload')[0]),
                contentType: false,
                cache: false,
                dataType: 'json',
                processData: false,
                success: function (data) {
                    var items = [];
                    $.each(data, function (key, val) {
                        console.log(val.upload['file_name']);
                if(val.upload.file_size ===2M){
                 alert("your file is 2M");
                  }
                    });
                }
            });

        });
    });
</script>

Thanks for help

5
  • 2
    Well - first problem I see is there is actually no array in the result. Check out this jsonmate.com permalink of your result - jsonmate.com/permalink/55af2de7aa522bae3683f13a . It's color-coded and you only have a nested object. You can get a value in code like data.img_pro.upload.file_name based on the hierarchy you see in that permalink I provided. Commented Jul 22, 2015 at 5:50
  • What do mean with index? The result is an object with sub-objects. Not an array. Do you mean the property-describer? Commented Jul 22, 2015 at 5:56
  • @michael.zech: The "property-describer" is commonly called the "key" Commented Jul 22, 2015 at 5:56
  • @ThisClark Thanks you very much it is working now.and I have to remember data array and Json thanks too much Commented Jul 22, 2015 at 6:07
  • @ThisClark If I got this Json data How can I using Children and Id? [{"id":1,"children":[{"id":1,"children":[{"id":325},{"id":325},{"id":325},{"id":325}]},{"id":1,"children":[{"id":326},{"id":326}]},{"id":1,"children":[]},{"id":1,"children":[]},{"id":1,"children":[]},{"id":1,"children":[{"id":330}]},{"id":1,"children":[]}]},{"id":332,"children":[]} Commented Jul 22, 2015 at 6:20

3 Answers 3

2

Just use the old fashioned way..

success: function (data) {    
    var image;
    if(!data.img_pro || !data.img_pro.upload) {
        return;
    }
    image = data.img_pro.upload;

    if(image.file_size > 2M) {
        //Do something..
        return;
    }

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

Comments

0

Use the "key" parameter which you assigned to the function (together with the "val" parameter): $.each(data, function (key, val) {

Comments

-1

jQuery has the $.inArray() method, which is similar to indexOf() method of JavaScript. Basically, it returns -1 when it didn't find any index.

For example:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.inArray demo</title>
  <style>
  div {
    color: blue;
  }
  span {
    color: red;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<div>"John" found at <span></span></div>
<div>4 found at <span></span></div>
<div>"Karl" not found, so <span></span></div>
<div>"Pete" is in the array, but not at or after index 2, so <span></span></div>

<script>
var arr = [ 4, "Pete", 8, "John" ];
var $spans = $( "span" );
$spans.eq( 0 ).text( jQuery.inArray( "John", arr ) );
$spans.eq( 1 ).text( jQuery.inArray( 4, arr ) );
$spans.eq( 2 ).text( jQuery.inArray( "Karl", arr ) );
$spans.eq( 3 ).text( jQuery.inArray( "Pete", arr, 2 ) );
</script>

</body>
</html>

(Source)

This example is from the jquery api website.

1 Comment

please don't copy paste another site content. you can provide link to that site..please take a tour of Stackoverflow usage.

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.