Home  >  Article  >  Web Front-end  >  How to implement remote communication function with ajax

How to implement remote communication function with ajax

php中世界最好的语言
php中世界最好的语言Original
2018-04-03 17:47:001683browse

This time I will show you how ajax can realize the remote communication function. What are the precautions for ajax to realize the remote communication function? The following is a practical case, let's take a look.

The example in this article shares the realization of remote communication with ajax for your reference. The specific content is as follows

First file:html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>ajax解决跨域问题</title>
  <script src="jquery-3.0.0.min.js" type="text/javascript"></script>
</head>
<body>
<script>
  $.ajax({
    type:"POST",
    url:"postDemo.php",
    data:{
      "url":"http://192.168.4.101:90/PHPStudy4/server.php",
      "username":"admin",
      "password":"admin",
    },success:function(data){
      var result=eval("("+data+")");
      console.log(result);
    }
  })
</script>
</body>
</html>

Second file: Server-side processing data

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-7-21
 * Time: 10:12
 */
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//  echo json_encode(array("111"=>"112"));
  if (isset($_POST["url"]) && isset($_POST["username"]) && isset($_POST["password"])) {
    $result = postDemo($_POST["url"], array("username" => $_POST["username"], "password" => $_POST["password"]));
    echo $result;
  } else {
    echo json_encode(array("msg2" => "!!!!!!!!!!!!!!!!!!!!!error!!!!!2"));
  }
} else {
  echo json_encode(array("msg" => "error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"));
}
function postDemo($url, $data)
{
  $query = http_build_query($data);
  $options = array(
    "http" => array(
      "header" => "Content-type: application/x-www-form-urlencoded\r\n" .
        "Content-length:" . strlen($query) . "\r\n" .
        "User-Agent:MyAgent/1.0/r/n",
      "method" => "POST",
      "content" => $query
    )
  );
  $content = stream_context_create($options);
  $result = file_get_contents($url, false, $content);
  return $result;
}
//echo postDemo("http://192.168.4.101:90/PHPStudy4/server.php",array("username"=>"admin","password"=>"admin"));
I believe I read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Ajax submits data to the backend database to implement user registration

How to achieve level 2 with Ajax combined with PHP Linkage

The above is the detailed content of How to implement remote communication function with ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn