美文网首页
跨域访问

跨域访问

作者: Mr丶T | 来源:发表于2019-11-15 16:51 被阅读0次

    1.ajax的jsonp格式

            $.ajax({ 

                type : "GET", 

                url : "http://127.0.0.1/raid/jquery_learning/ajax_learning/php/index.php?number="+$("#keyword").val(), 

                dataType : "jsonp", 

                jsonp : "callback", 

                success : function(data) { 

                        if (data.success) { 

                                $("#searchResult").html(data.msg); 

                        } else { 

                                $("#searchResult").html("出现错误"+data.msg); 

                        } 

                }, 

                error : function(jqXHR) { 

                        alert("发生错误"+jqXHR.status); 

                } 

           }) 

    2.html的

            header("Access-Control-Allow-Method:POST,GET");

            header("Access-Control-Allow-Origin: *");

    3.多个地址跨域访问

            //跨域访问的时候才会存在此字段 

            $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';   

            $allow_origin = array(   

                'http://www.a.com',   

                'http://www.b.com' 

            );   

            if(in_array($origin, $allow_origin)){   

                header('Access-Control-Allow-Origin:'.$origin);   

                header('Access-Control-Allow-Methods:POST');   

                header('Access-Control-Allow-Headers:x-requested-with,content-type');   

            }

    相关文章

      网友评论

          本文标题:跨域访问

          本文链接:https://www.haomeiwen.com/subject/ldusictx.html