It depends on the city. In Beijing you can apply online through the Beijing Public Security Bureau portal; in Shanghai online application is not available and you must apply in person. For other cities, call 12367 to ask.
Nationwide (all cities)
Depends on the city
Whether you can apply for a no-criminal-record certificate online depends on your city.
Other cities
For other cities, there is no uniform nationwide online service. The practical way to find out is to call the national exit-entry service hotline 12367 and ask whether your city supports online application for a foreigner's no-criminal-record certificate.
Local variations
Beijing: online available
In Beijing, foreigners who have resided in Beijing for a cumulative total of 180 days or more can apply online through the Beijing Public Security Bureau's online portal (gaj.beijing.gov.cn): go to Government Services, then Entry-Exit Business, then 'foreigners applying for a criminal record check'. The result is collected as an electronic certificate from the same portal. Offline application is also possible at any district exit-entry administration office, but the online route saves a trip.
Shanghai: in person only
In Shanghai, online application is not available for this certificate. You must apply in person at the exit-entry administration office, and no appointment is required.