Photometric studies of composite stellar systems. IV - Infrared photometry of globular clusters in M31 and a comparison with early-type galaxies