Ajax cross-Domain request solution

Source: Internet
Author: User

Hello everyone, today we learned JS cross-domain request solution, because JS in the same Origin policy, when the request different protocol name, different port number, different hostname under the file, will violate the same-origin policy, cannot request success! Cross-domain processing is required!

Scenario one, the background PHP settings,

The front desk does not need any settings, in the background of the requested PHP file, write a header

Header ("access-control-allow-origin:*");

That allows those domain names to request this PHP file * indicates that all domain names are allowed

This is the best solution, because it is set in the background, not public, so more secure,

Scenario two, using SRC request +jsonp to implement cross-domain

The *① with the SRC attribute has a cross-domain feature, all of which can request background data using the SRC attribute of the script tag.

* <script src= "http://127.0.0.1/json/php" type= "Text/javascript" charset= "Utf-8" > </script>

*② since SRC successfully loads the data, the loaded content is placed directly into the script tag,

* So the back-end directly returns a JSON string that will no longer be parsed in the script tag.

* Therefore the background should return a callback function name to the foreground and call the JSON string as a parameter

* Return from background PHP file China: echo "CallBack ({$json})";

③ The callback function is called directly in the script tag when the foreground receives the returned callback function, it is necessary to declare such a callback function as a callback for the successful request.

eg

$.ajax ({                method:"Post",                URL:"http://127.0.0.1/json/php",                dataType:" Jsonp ",                success:function(data) {                    console.log (data);                    Console.log ($str [1].name)                }                        );

Scenario three, the Ajax implementation of jquery JSONP

① in Ajax request Yes, set datatype to "JSONP"

② background return is, still need to return callback function. However, Ajax in the Send request is the default use GET request callback function name to the background, background can use echo $_get[' callback '] to remove the callback function name, so the foreground can use the Ajax success function as a successful callback.

Echo ' {$_get[' callback '} ({$str}) ";

③ back in the background, Ajax can still use success as a successful callback function;

Success:function (data) {}

Of course, the backstage can also be casually return a callback function name, echo "CallBack ({$str})",

The function is automatically called by the foreground whenever the request succeeds. Similar to the ②③ step of the second article

<script src= "http://127.0.0.1/json/php" type= "Text/javascript" charset= "Utf-8" >

Ajax cross-Domain request solution

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.