Analysis of JSP instance of Ajax cross domain solution in PHP

Time:2021-5-4

In this paper, an example of Ajax cross domain solution in PHP, jsonp, is introduced. The details are as follows:

First of all, what’s the difference between JSON and jsonp?

JSON is a text-based way of data exchange, or a format to describe data.

var person = {
  "name": "test",
  "age": "25",
  "Sex": "male"
};

var data = [1, 2, 3, 4, 5];

Jsonp is an unofficial cross domain data interaction protocol, which allows users to pass a callback parameter to the server, and then the server will use the callback parameter as the function name to wrap the JSON data when it returns the data, so that the client can customize its own function to automatically process the returned data.

For example, I refer to a B.js of b.com on the website of a.com, but this cross domain reference will not produce errors, indicating that calling JS file is not affected by cross domain.


<script type="text/javascript" src="https://www.b.com/b.js"></script>

Then we add the following code in B.js to see if it can be executed


alert("I from b");

Make sure it’s executable.

So if we create a JS function on a.com and call it in B.js under b.com, is that feasible?

a. The index.html is as follows:


<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <script type="text/javascript">
    function a(data) {
      alert("I from a \r\n" + "data:" + data);
    }
  </script>
  <script type="text/javascript" src="https://www.b.com/b.js"></script>
</body>
</html>

b. The B.js under com is as follows:


a("from b");

The above is also executable. We can see that the data in B.js is passed to function a correctly.

The problem is coming again. The function name created in a.com must be consistent with the function name that is called in B.js. Then how can the b.com server know the name of the function and pass it through the address bar, plus a callback= function name. Of course, the name of callback can be changed, but if everyone names it like this, it’s a convention.

a. The index.html is as follows:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <script type="text/javascript">
    function a(data) {
      alert("uid :" + data.uid + "name :" + data.name);
    }
  </script>

  <!--  Note that B.js is changed to b.php -- >
  <script type="text/javascript" src="https://www.b.com/b.php?callback=a"></script>
</body>
</html>

b. The b.php under. Com is as follows:

<?php
$callback = !empty($_GET['callback']) ? trim($_GET['callback']) : '';

if(!empty($callback)) {
  $data = json_encode(array(
    'uid' => 1,
    'name' = >'test ',
  ));

  echo "{$callback}({$data});";
}

The above is also executable. When you know the name of the callback function, the b.com server handles the data well, and then stitches the output through strings.

Jsonp support has been provided in jQuery. The index.html under a.com is as follows:


<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <div></div>
  <script type="text/javascript" src="https://www.b.com/jquery.js"></script>
  <script type="text/javascript">
  $.ajax({
    dataType: "jsonp",
    url: "http://www.b.com/b.php",
    jsonp: "callback",
    success: function(data) {
      $(".info").text("uid:" + data.uid + " name:" + data.name);
    }
  });
  </script>
</body>
</html>

The cross domain policy restrictions are as follows:

URL explain Allow communication
http://www.a.com/a.js
http://www.a.com/b.js
Under the same domain name allow
http://www.a.com/lab/a.js
http://www.a.com/script/b.js
Different folders under the same domain name allow
http://www.a.com:8000/a.js
http://www.a.com/b.js
Same domain name, different ports not allow
http://www.a.com/a.js
https://www.a.com/b.js
Same domain name, different protocol not allow
http://www.a.com/a.js
http://127.0.0.100/b.js
Domain name and domain name corresponding IP not allow
http://www.a.com/a.js
http://script.a.com/b.js
The main domain is the same, but the subdomains are different not allow
http://www.a.com/a.js
http://a.com/b.js
The same domain name, different secondary domain names (as above) not allow
http://www.a.com/a.js
http://www.b.com/b.js
Different domain names not allow

More information about PHP can be found in the following topics: PHP + Ajax skills and application summary, PHP network programming skills summary, PHP string usage summary, PHP + MySQL database operation introductory course and PHP common database operation skills summary

I hope this article is helpful for PHP programming.

Recommended Today

NLP in financial market — emotional analysis

By Yuki TakahashiCompile VKSource: towards Data Science Since the launch of alexnet on Imagenet, the deep learning of computer vision has been successfully applied to various applications. On the contrary, NLP has been lagging behind in the application of deep neural network. Many applications that claim to use artificial intelligence usually use some rule-based algorithm […]