When ajax cross-origin requests post data to the php program, the php program always executes the solution twice, ajaxpost
The php program is deployed on IIS7. When ajax submits data, it encounters two problems: one is cross-origin, and the other is always executed twice.
The solution to the first problem is from Baidu. Just add the following lines of code:
Header ('access-Control-Allow-Origin :*');
Header ("Access-Control-Allow-Headers: x-requested-with, content-type ");
Header ('access-Control-Allow-Methods: OPTIONS, POST, get ');
In fact, you can also directly set it in iis, without adding code, directly select the site, and then click the http Response Header on the right to directly configure it.
The second problem was that Baidu had never produced any results, so I had to use google for FQ. Then I understood it, followed the clues, and finally understood it.
In CORS. net cross-origin solutions, This is the introduction (http://jingyan.baidu.com/article/03b2f78c190ad25ea237ae33.html)
The same request requires two methods with the same request address. One method is marked as "POST", the other is marked as "OPTIONS", and the entity method is marked as "POST, here, the "OPTIONS" method does not need to perform any logic operation and the return value is void (in fact, the OPTIONS request is the browser validation. When a POST request with parameters is sent, the browser will first send an OPTIONS request, the post request is initiated only when the request header information is consistent with the response header information)
Packet Capture found that, indeed, the browser generated two requests, OPTIONS service/service. php HTTP/1.1 and POST/service. php HTTP/1.1
As mentioned in this article, void is directly returned when OPTIONS is requested. However, when php is configured, you can directly restrict the action. I tried it and it was useful. Now, ajax cross-origin is only executed once by the php program.
The configuration method in iis is as follows: Find the php Program site, select server settings, click the handler ing on the right, find php, click Edit, and in the displayed dialog box, click request restrictions, select a predicate, click one of the following predicates, and follow the prompts to input GET and POST. I used to save time and directly use the default option (all predicates), So I encountered this problem.