This article mainly introduces the relevant information on server-side configuration to implement AJAX cross-domain requests. Friends in need can refer to it
I always thought that AJAX cross-domain was an insurmountable gap. Recently, I discovered that it turns out that on the server side AJAX cross-domain requests can be allowed by sending header information.
PHP code example:
header('Access-Control-Allow-Origin:*'); header('Access-Control-Allow-Headers:X-Requested-With');
After testing, the webkit-based browser successfully implemented cross-domain requests.
IE does not support it as always~ It seems that this powerful function can only be enjoyed on the mobile terminal
In addition, it should be noted that the * in the first line of configuration indicates that all referrs are allowed. Cross-domain requests can be configured as a specific domain name if you do not want to fully open them. This ensures that only cross-domain requests from specific domain names can succeed. For example:
header('Access-Control-Allow-Origin:http://www.test.com');
The meaning of the second line of configuration is to allow asynchronous AJAX requests, because asynchronous AJAX will automatically send this header information, and the server generally determines that it is an AJAX request by accepting the header information. Generally, the AJAX requests we send are asynchronous.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
AJAX processing method for XML returned by the server
AJAX simple asynchronous communication example analysis
Detailed explanation of AJAX mechanism and cross-domain communication
The above is the detailed content of Server-side configuration to implement AJAX cross-domain requests. For more information, please follow other related articles on the PHP Chinese website!