js仿表单提交post

<body>
<script type="text/javascript">
function post(URL, PARAMS) {
    window.alert('xxx');
    var temp = document.createElement('form');
    temp.action = URL;
    temp.method = 'post';
    temp.style.display = 'none';
    for (var x in PARAMS) {
        var opt = document.createElement('textarea');
        opt.name = x;
        opt.value = PARAMS[x];
        temp.appendChild(opt);
    }·
    document.body.appendChild(temp);
    temp.submit();
    return temp;
}
post('index1.php', {loginType:'facebook',username:'userID'});
</script>
</body>

GET请求

var httpRequest = new XMLHttpRequest();//第一步:建立所需的对象
        httpRequest.open('GET', 'url', true);//第二步:打开连接  将请求参数写在url中  ps:"./Ptest.php?name=test&nameone=testone"
        httpRequest.send();//第三步:发送请求  将请求参数写在URL中
        /**
         * 获取数据后的处理程序
         */
        httpRequest.onreadystatechange = function () {
            if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                var json = httpRequest.responseText;//获取到json字符串,还需解析
                console.log(json);
            }
        };

POST请求

var httpRequest = new XMLHttpRequest();//第一步:创建需要的对象
httpRequest.open('POST', 'url', true); //第二步:打开连接
httpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");//设置请求头 注:post方式必须设置请求头(在建立连接后设置请求头)
httpRequest.send('name=teswe&ee=ef');//发送请求 将情头体写在send中
/**
 * 获取数据后的处理程序
 */
httpRequest.onreadystatechange = function () {//请求后的回调接口,可将请求成功后要执行的程序写在其中
    if (httpRequest.readyState == 4 && httpRequest.status == 200) {//验证请求是否发送成功
        var json = httpRequest.responseText;//获取到服务端返回的数据
        console.log(json);
    }
};

POST发送json

var httpRequest = new XMLHttpRequest();//第一步:创建需要的对象
httpRequest.open('POST', 'url', true); //第二步:打开连接/***发送json格式文件必须设置请求头 ;如下 - */
httpRequest.setRequestHeader("Content-type","application/json");//设置请求头 注:post方式必须设置请求头(在建立连接后设置请求头)var obj = { name: 'zhansgan', age: 18 };
httpRequest.send(JSON.stringify(obj));//发送请求 将json写入send中
/**
 * 获取数据后的处理程序
 */
httpRequest.onreadystatechange = function () {//请求后的回调接口,可将请求成功后要执行的程序写在其中
    if (httpRequest.readyState == 4 && httpRequest.status == 200) {//验证请求是否发送成功
        var json = httpRequest.responseText;//获取到服务端返回的数据
        console.log(json);
    }
};

判断客户端浏览器类型

所以通过识别 MicroMessenger 这个关键字来确定是否微信内置的浏览器了。钉钉浏览器检测头关键字 DingTalk
通过JavaScript 判断
<script>
function is_weixin(){
var ua = navigator.userAgent.toLowerCase();
if(ua.match(/MicroMessenger/i)=="micromessenger") {
return true;
} else {
return false;
}}
</script>

通过 PHP 判断
<?php
function is_weixin(){ 
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {// 查找useragent 是否包含字符串
return true;
} 
return false;}
?>

网页页面跳转

<?php
header('location:'.$menu_url.'?s='.$encrypt_url.'&t='.$time);
// header('location:http://oa.ranjuan.cn/login.php?s='.$encrypt_url.'&t='.$time);
?>
<?php
// 在Web上显示一段时间,隔一定秒数后,自动跳转到其他的Web页
echo "<meta http-equiv='refresh' content='0;URL=new_page.php' />";
// < meta http-equiv="Refresh" content="秒数; url=跳转的文件或地址" > 
?>

<script language="javascript" type="text/javascript">
window.location.href="http://127.0.0.1:8181/file/index.php";
</script>
';
<div class="artSign" onclick="view('https://test.com',2)" ></div>
<script>
function view(url,id){
    var httpRequest = new XMLHttpRequest();//第一步:建立所需的对象
        httpRequest.open('GET', '?view='+id, true);//第二步:打开连接  将请求参数写在url中  ps:"./Ptest.php?name=test&nameone=testone"
        httpRequest.send();//第三步:发送请求  将请求参数写在URL中
    window.open(url,'_blank');
}
</script>

后端允许跨域请求(参考)

<?php
header('Content-Type: text/html;charset=utf-8');
//header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
header('Access-Control-Allow-Methods:POST,GET'); // 允许请求的类型
header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
?>
// 在web.config中的<system.webServer>中配置如下代码
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol> 
//允许跨域访问
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS,DELETE,PUT");
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type");
最后编辑: admin  文档更新时间: 2021-03-16 21:53   作者:admin