Server-side configuration to implement AJAX cross-domain requests

亚连
Release: 2018-05-25 13:51:37
Original
1177 people have browsed it

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');
Copy after login

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');
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!