JSONP跨域总结和实践

同源策略

ajax的出现虽然促进了web的发展,但是也带来了安全性方面的问题。

比如我们可以通过xxs将脚本注入到目标网页中,实时的向我们的服务器发送用户的所有操作。

为了保证前端的安全性,推出了同源策略,即无法通过xhr向其他服务器发送信息。

下面是node服务端代码,作为一个简单的http静态服务器。

var http = require("http");
var url = require('url');
var path = require('path');
var fs = require('fs');

var MIME = {

    "css": "text/css",

    "gif": "image/gif",

    "html": "text/html",

    "ico": "image/x-icon",

    "jpeg": "image/jpeg",

    "jpg": "image/jpeg",

    "js": "text/javascript",

    "json": "application/json",

    "pdf": "application/pdf",

    "png": "image/png",

    "svg": "image/svg+xml",

    "swf": "application/x-shockwave-flash",

    "tiff": "image/tiff",

    "txt": "text/plain",

    "wav": "audio/x-wav",

    "wma": "audio/x-ms-wma",

    "wmv": "video/x-ms-wmv",

    "xml": "text/xml"

}

var routeHandle = {};

function route(handle, pathname, request, response) {
    // path.extname(relPath) 获取文件后缀名
    if (path.extname(pathname)) {
        // 静态文件 处理
        doStaticFile(pathname, response);
    }
    else {
        // action 处理
        response.writeHead(200,{ 'Content-Type': 'text/plain;charset=utf-8' });
        response.write("成功");
        response.end();
    }
}

function doStaticFile(relPath, response) {
    relPath = relPath.indexOf('/') == 0 ? relPath.replace('/', '') : relPath;
    fs.exists(relPath, function (exists) {
        if (!exists) {
            response.writeHead(404, { 'Content-Type': 'text/plain;;charset=utf-8' });

            response.write("请求的路径不存在:" + relPath);

            response.end();
        } else {
            fs.readFile(relPath, 'binary', function (err, file) {
                if (err) {
                    // 服务器异常
                    response.writeHead(500, { 'Content-Type': 'text/plain;;charset=utf-8' });

                    response.end();
                } else {
                    // 返回静态文件
                    var suffix = path.extname(relPath);

                    // 由于extname返回值包含”.”,所以通过slice方法来剔除掉”.”
                    var mime = MIME[suffix.slice(1)] || 'text/plain';

                    response.writeHead(200, { 'Content-Type': mime });

                    response.write(file, "binary");

                    response.end();
                }
            })
        }
    })

}

http.createServer(function (request, response) {

    var pathname = url.parse(request.url).pathname;

    route(routeHandle, pathname, request, response);

}).listen(10000, '127.0.0.1');

下面是客户端的测试代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        var orderHost = "http://127.0.0.1:10000";

        var request = new XMLHttpRequest();
        request.open("get",orderHost+"/test",true);
        request.onreadystatechange = function(){
            if(request.readyState == 4){
                console.log(request);
            }
        }
        request.send(null);
    </script>
</body>
</html>

浏览器通过 http://127.0.0.1:10000/index.html 访问

同域请求

访问成功,这次试下 http://localhost:10000/index.html

跨域请求

非常好,成功触发同源策略。

jsonP跨域

同源策略限制的是xhr请求,对标签(script、img、link等)并没有限制。

jsonP跨域通过script标签的src来实现跨域。

利用script标签的自执行特点,在服务端生成js代码,调用客户端传来的回调函数,将数据传入进去。

var querystring = require('querystring');
...

function route(handle, pathname, request, response) {
    // path.extname(relPath) 获取文件后缀名
    if (path.extname(pathname)) {
        // 静态文件 处理
        doStaticFile(pathname, response);
    }
    else {
        // action 处理
        response.writeHead(200, { 'Content-Type': 'text/javascript;charset=utf-8' });
        var query = url.parse(request.url).query;
        var param = querystring.parse(query); // 序列化成对象

        var data = {
            say: '跨域成功'
        }

        // 回调方法
        response.write(
            '(function(){'
            + param['callback'] + '(' + JSON.stringify(data) + ')'
            + '})()'
            );
        response.end();
    }
}

客户端

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

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<body>
    <script>
        var orderHost = "http://127.0.0.1:10000";

        var script = document.createElement("script");
        script.src = orderHost+"/test?callback=testFn";

        function testFn(data){
           console.log(data.say);
        }

        document.getElementsByTagName("head")[0].appendChild(script);
    </script>
</body>

</html>

最终结果,成功实现跨域。

这里写图片描述

这里写图片描述

这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值