跨域解决 Access to XMLHttpRequest at

1、请求添加这一行

contentType: "application/x-www-form-urlencoded",

示例

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>显示小区信息</title>
<script src="http://47.94.33.88/js/jquery-3.5.1.js"></script>
<script src="http://47.94.33.88/js/vue.js"></script>

</head>
<body>
<div id="app">

<input v-model="username" class="money"></input>

</div>

<script>

const header = new Vue({
    el: '#app',
    data: {
        'username': ""
    },
    created: function () {
        var that = this;
        $.ajax({
            contentType: "application/x-www-form-urlencoded",
            url: "http://47.94.33.88/ajax/ajax.php",
            type: 'POST',
            data: {
                'usrename': that.username
            },
            success: function (data) {
                var json = JSON.parse(data);
                that.username = json;
            }
        })
    }

})

</script>

</body>
</html>
2、php页面
添加这一行,如果是PHP网站,则在入口处添加些即可。
header(‘Access-Control-Allow-Origin: *’);

<?php
header('Access-Control-Allow-Origin: *');
echo json_encode("hello");
?>

标签:

发表评论