We study the formation of multiple populations in globular clusters (GC), under the hypothesis that stars in the second generation formed from the winds of intermediate-mass stars, ejected during the asymptotic giant branch (AGB) phase, possibly diluted with pristine gas, sharing the same chemical composition of first-generation stars. To this aim, we use the recent APOGEE data, which provide the surface chemistry of a large sample of giant stars, belonging to clusters that span a wide metallicity range. The APOGEE data set is particularly suitable to discriminate among the various pollution scenarios proposed so far, as it provides the surface abundances of Mg and Al, the two elements involved in a nuclear channel extremely sensitive to the temperature, hence to the metallicity of the polluters. The present analysis shows a remarkable agreement between the observations and the theoretical yields from massive AGB stars. In particular, the observed extension of the depletion of Mg and O and the increase in Al is well reproduced by the models and the trend with the metallicity is also fully accounted for. This study further supports the idea that AGB stars were the key players in the pollution of the intra-cluster medium, from which additional generations of stars formed in GC.