ajax實現遠程通訊_AJAX相關

來源:互聯網
上載者:User

本文執行個體為大家分享了ajax實現遠程通訊,供大家參考,具體內容如下

第一個檔案: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>

 第二個檔案:伺服器端處理資料 

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

其中"url":"http://192.168.4.101:90/PHPStudy4/server.php",這個url就是我們向遠端的訪問地址.

 以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.