When Ajax cross-domain to the PHP program post data, the PHP program always executes two times the workaround

Source: Internet
Author: User

PHP program is deployed on IIS7, Ajax submitted data, encountered two problems, one is cross-domain, a PHP program will always be executed two times.

The first problem of the solution, is Baidu out, add the following lines of code can be:

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 in the IIS settings, do not add code, directly select the site, and then point to the right of the HTTP response header, in the inside directly with the good, you can.

The second problem, has not been Baidu results, had to FQ with Google, a bit on the see, and then follow the clues to find, finally clear.

As described in the cors. NET cross-domain solution (http://jingyan.baidu.com/article/03b2f78c190ad25ea237ae33.html)

The same request requires two methods of the same request address, one labeled "POST" and the other labeled "Options", and the entity method is the method of marking "post", where "OPTIONS" The method does not need to do any logic and the return value is void (in fact, the options request is the browser's checksum, and when the POST request with the parameter is sent, the browser sends an options request to verify the request The header information is consistent with the response header information before actually initiating the POST request)

Grab bag found, indeed, the browser produced two requests, OPTIONS service/service.php http/1.1 and post/service/service.php http/1.1

In this case, I would like to say, let the request options, directly return void, but when you see the configuration of PHP, you can directly restrict the action. Try it, sure enough, now Ajax cross-domain, PHP program only executes once.

IIS configuration method is as follows: Find the PHP program site, you can also choose Server Settings, point to the right of the handler map, find PHP, click Edit, in the pop-up dialog, point request limit, then select the predicate, one of the following predicates, according to the input get,post, I was before for the sake of convenience, It is the default option (all predicates), so this problem is encountered.

When Ajax cross-domain to the PHP program post data, the PHP program always executes two times the workaround

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.