1、后台参考ajax jsonp写,返回一个callback,结果全是html,需要手动解析一下

            this.$axios(
                {
                    method: 'post',
                    url: '/apis/tencent/jsonp/index.php',
                    data: querystring.stringify({'name': "223", id: 'aaa'}),
                    headers: {'Content-Type': 'application/x-www-form-urlencoded', 'token': '12ssDDdd'}
                }).then(function (res) {
                let num1 = res.data.indexOf('({')
                let num2 = res.data.indexOf('})')
                let resultData = JSON.parse(res.data.substring(num1 + 1, num2 + 1))
                console.log(resultData)
            }).catch(function (error) {
                console.log(error)
            })

2、PHP后台

<?php
$name = $_POST['name'];
$id = $_POST['id'];
$callback = $_POST['callback'];

指定允许其他域名访问
//header("Access-Control-Allow-Origin:*");
 响应类型
//header("Access-Control-Allow-Methods:POST,GET,PUT,DELETE,OPTIONS");
 响应头设置
//header("Access-Control-Allow-Headers:x-requested-with,content-type");
//
//header('Access-Control-Allow-Headers:TOKEN,test');
header("Content-Type:application/x-www-form-urlencoded,charset=utf-8");
if (true) {
    $data = array(
        'name' => $name,
        'id' => $id,
        'msg'=>'返回成功'
    );
    $jsondata = json_encode($data);
//    echo $jsondata;
//    exit($callback."($jsondata)");
    echo $callback."(".$jsondata.")";

} else {
    $data = array(
        'msg' => '请求不合法',
    );
    $jsondata = json_encode($data);
//    echo $jsondata;
//    exit($callback . "($jsondata)");
    echo $callback."(".$jsondata.")";
}
?>


 

Logo

前往低代码交流专区

更多推荐