Home > Article > Web Front-end > Ajax cross-domain methods—jsonp and CROS
In the past, I only knew that the solution to cross-domain ajax could only be solved through jsonp. Recently, when uploading videos locally, I needed to upload the video to Youku and YouTube. If I uploaded it directly through the post method, I could only see the upload at the bottom of the browser. Progress and user experience are very poor. If you use ajax to upload, you can customize the upload progress and display the upload speed. However, since the video is uploaded to a third-party video service provider, the first problem encountered is cross-domain.
CORS can solve this problem.
CORS is undoubtedly more advanced, convenient and reliable than JSONP.
1. JSONP can only implement GET requests, while CORS supports all types of HTTP requests.
2. Using CORS, developers can use ordinary XMLHttpRequest to initiate requests and obtain data, which has better error handling than JSONP.
3. JSONP is mainly supported by old browsers, which often do not support CORS, while most modern browsers already support CORS.
PHP: header(""Access-Control-Allow-Origin: *"");
html:
If CORS contains a 302 jump, the URL after the 302 jump needs to also contain the CORS header request.
Currently, IE8 and above and other mainstream browsers already support CORS cross-domain. I believe this technology will be very practical in the future.