圆月山庄资源网 Design By www.vgjia.com

本文实例为大家分享了ajax实现远程通信,供大家参考,具体内容如下

第一个文件:html 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>ajax解决跨域问题</title>
  <script src="/UploadFiles/2021-04-02/jquery-3.0.0.min.js">

 第二个文件:服务器端处理数据 

<"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"));

其中"url":"http://192.168.4.101:90/PHPStudy4/server.php",这个url就是我们向远端的访问地址.

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
ajax,远程通信

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com