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)端的訪問地址.
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決ajax返回驗(yàn)證的時(shí)候總是彈出error錯(cuò)誤的方法
這篇文章主要介紹了解決ajax返回驗(yàn)證的時(shí)候總是彈出error錯(cuò)誤的方法,感興趣的小伙伴們可以參考一下2016-01-01
Ajax提交參數(shù)的值中帶有html標(biāo)簽不能提交成功的解決辦法(ASP.NET)
這篇文章主要介紹了Ajax提交參數(shù)的值中帶有html標(biāo)簽不能提交成功的解決辦法(ASP.NET),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-12-12
配置Chrome支持本地(file協(xié)議)的AJAX請(qǐng)求
這篇文章主要為大家詳細(xì)介紹了配置Chrome支持本地(file協(xié)議)的AJAX請(qǐng)求 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

