In the past, we only knew that ajax cross-origin can only be solved through the jsonp method. When uploading a video locally recently, we need to upload the video to Youku and youtube. If we use the post method directly, you can only see the upload progress below the browser, and the user experience is poor. If you use ajax to upload, you can customize the upload progress and display the upload speed. However, due to the upload to a third-party video service provider, the first problem is cross-origin. In the past, we only knew that ajax cross-origin can only be solved through the jsonp method. When uploading a video locally recently, we need to upload the video to Youku and youtube. If we use the post method directly, you can only see the upload progress below the browser, and the user experience is poor. If you use ajax to upload, you can customize the upload progress and display the upload speed. However, due to the upload to a third-party video service provider, the first problem is cross-origin.
CORS can solve this problem.
CORS is more advanced, convenient, and reliable than JSONP.
1. JSONP can only implement GET requests, while CORS supports all types of HTTP requests.
2. With CORS, developers can use common XMLHttpRequest to initiate requests and obtain data, which provides better error handling than JSONP.
3. JSONP is mainly supported by old browsers, which often do not support CORS, and most modern browsers already support CORS.
In PHP: header ("Access-Control-Allow-Origin :*"");
In html:
If CORS contains a 302 jump, the URL after the 302 jump also contains the CORS header request.
Currently, both IE8 and other mainstream browsers support CORS. We believe this technology will be very useful in the future.