/head body script //ajax跨域请求 function。ajax跨域解决方法( 二 )。" />

ajax跨域解决方法( 二 )


【ajax跨域解决方法】 <meta charset="UTF-8">
<title></title>
<script src=https://vvvtt.com/article/"https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</head>
<body>

<script>
//ajax跨域请求
    function getAjax() {
        //基于jQuery的跨域请求
        $.ajax({
            //设置请求类型
            type:"get",
            //请求超不时间
            timeout:5000,
            //设置请求地址
            url: 'http://localhost:8080/bean?callback=?',
            //设置需要发送的数据
            data: {id:2,name:'李四',sex:'男'},
            //界说此请求为跨域请求
            dataType: 'jsonp',
            success: function (data) {
console.log(data.id+data.name+data.sex);
            },
            error: function (data) {
               console.log(data)
            }
        })
    }
    
    //发送请求
    getAjax();
</script>
</body>
</html>

ajax跨域解决方法



3第三步:测试 。
1、启动被挪用办事 , 并测试
2、启动挪用办事 , 打开需要挪用的页面如下如下所示:

ajax跨域解决方法



ajax跨域解决方法



第三
:总结1第一步:还可以利用jquery插件jquery.jsonp.js实现跨域
下载地址:https://github.com/congmo/jquery-jsonp/tree/master/src
function  jsontest(){
    //下载引入引入jquery.jsonp.js   
    //https://github.com/congmo/jquery-jsonp/tree/master/src
$.jsonp({
 url: 'http://localhost:8080/bean?callback=?',
 "success": function(data) {
  console.log(data.id+data.name+data.sex);
 },
 "error": function(d,msg) {
   console.log(data)
 }
});
    }

ajax跨域解决方法



2第二步:总结 。
现实上我们在参数中传输callback参数与 , jquery就认为你在发送跨域请求 , 此时你不界说dataType: 'jsonp',请求仍然没有问题 。

注重事项jdk 1.8

以上内容就是ajax跨域解决方法的内容啦 , 希望对你有所帮助哦!

猜你喜欢