We carried out a model-independent search for light scalar (s) and pseudoscalar axionlike (a) particles that couple to two photons by using the high-energy CERN SPS H4 electron beam. The new particles, if they exist, could be produced through the Primakoff effect in interactions of hard bremsstrahlung photons generated by 100 GeV electrons in the NA64 active dump with virtual photons provided by the nuclei of the dump. The a(s) would penetrate the downstream HCAL module, serving as a shield, and would be observed either through their a(s)→γγ decay in the rest of the HCAL detector, or as events with a large missing energy if the a(s) decays downstream of the HCAL. This method allows for the probing of the a(s) parameter space, including those from generic axion models, inaccessible to previous experiments. No evidence of such processes has been found from the analysis of the data corresponding to 2.84×10^{11} electrons on target, allowing us to set new limits on the a(s)γγ-coupling strength for a(s) masses below 55 MeV.