天天看點

jsonp跨域請求360資料亂碼解決辦法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="js/jquery-1.12.4.min.js"></script>
    <script>
    $(function(){
        $('input').keyup(function(){
            var vals = $(this).val()
            $.ajax({
                url:'https://sug.so.360.cn/suggest',
                type:'get',
                dataType:'jsonp',
                data:{word:vals,
                    // 亂碼在這加編碼
                    encodein: 'utf-8',
                    encodeout: 'utf-8'}
            }).done(function(dat){
                var str = ''
                for(var i=0;i<dat.s.length;i++)
                {
                    str += '<li>' + dat.s[i] +'</li>'
                }
                $('ul').html( str )
            })
            })
    })
    </script>
</head>
<body>
    <input type="text">
    <ul></ul>
</body>
</html>