javascript 跨域请求

post

方法一

服务端

header('Access-Control-Allow-Origin:*');
$data = json_encode(array("id" => "1", "name" => "tom"));
echo $data;

前端


$.ajax({
    type: "POST",
    url: "http://127.0.0.1/~chenjiebin/mycode/php/crossdomain/header.php",
    dataType: "json",
    success: function(data) {
        console.log(data);
    }
});

方法二

get请求跨域使用jsonp

猜你喜欢

转载自blog.csdn.net/benben0729/article/details/83475096