您好,登錄后才能下訂單哦!
小編給大家分享一下ajax怎么實(shí)現(xiàn)遠(yuǎn)程通信,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
本文實(shí)例為大家分享了ajax實(shí)現(xiàn)遠(yuǎn)程通信,供大家參考,具體內(nèi)容如下
第一個(gè)文件: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>
第二個(gè)文件:服務(wù)器端處理數(shù)據(jù)
<?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",這個(gè)url就是我們向遠(yuǎn)端的訪問地址.
ajax是一種在無需重新加載整個(gè)網(wǎng)頁(yè)的情況下,能夠更新部分網(wǎng)頁(yè)的技術(shù),可以通過在后臺(tái)與服務(wù)器進(jìn)行少量數(shù)據(jù)交換,使網(wǎng)頁(yè)實(shí)現(xiàn)異步更新。
看完了這篇文章,相信你對(duì)“ajax怎么實(shí)現(xiàn)遠(yuǎn)程通信”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。