Matthew
Matthew

Reputation: 2246

Ajax 404 (Not Found) Looping through a loop

I'm trying NOT to repeat myself with the SAME code, so I thought I would use the jQuery .each() method. Every time I reload the webpage, I get this ajax error:

enter image description here

The loop keeps turning it into an object. Here's my code:

var counterColors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink',    'gray', 'brown'], $currentCounterColor;

$(counterColors).each(function() {
    $currentCounterColor = $(this);
        (function($) {
            $(document).ready(function() {
                $.ajaxSetup({
                    cache: false,
                    success: function() {
                        $('#count_' + $currentCounterColor).fadeIn(300);
                    }
                });
                $currentCounterColor.load('./' + $currentCounterColor + '.php');
               var refreshId = setInterval(function() {
               $currentCounterColor.load('./' + $currentCounterColor + '.php');
              }, 60000);
          })
     })(jQuery);
});

I have 9 IDs like so:

#count_red
#count_orange
#count_yellow
... etc

Then I have 9 PHP files like so:

red.php
orange.php
yellow.php

I'm not familiar w/ this error..

Upvotes: 0

Views: 121

Answers (1)

Mritunjay
Mritunjay

Reputation: 25892

I think you should try like bellow. Because you are not taking any parameter to each and using this to refer. this will return an String like {0:"r",1""e",2:"d"}.

So when you are concatenating that it will return #count_[object Object] not #count_red.

I am just telling how you can proceed.

var counterColors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink',    'gray', 'brown'], $currentCounterColor;

$(counterColors).each(function(index,value) {
    $currentCounterColor = value;
    $('#count_' + $currentCounterColor).fadeIn(300); // here first time it will call the #count_red.load()

});

But in line bellow I'm not sure what you want to say

$currentCounterColor.load('./' + $currentCounterColor + '.php');

So just have look on the way, and change your implementation accordingly.

Upvotes: 1

Related Questions