When ajax cross-origin requests post data to the php program, the php program always executes the solution twice, ajaxpost

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.