zoukankan      html  css  js  c++  java
  • 用nodejs实现json和jsonp服务

    一、JSON和JSONP

          JSONP的全称是JSON with Padding,由于同源策略的限制,XmlHttpRequest只允许请求当前源(协议,域名,端口)的资源。如果要进行跨域请求,我们可以通过使用html的script标记来进行跨域请求,并在相应中返回要执行的script代码,其中可以直接使用JSON传递javascript对象。这种跨域的通讯方式成为JSONP。

          由此我们可以看出两者的区别:

          json: 一种轻量级的数据格式。

          jsonp:为实现跨域,而采用的一种脚本注入方法。

          备注:要了解更多json,可以参见我原先写的一篇介绍json的文章:《JSON那些事》

          二、实现

          为了简单起见,我们要读取数据都是

    var data = {'name': 'jifeng', 'company': 'taobao'};

     1. 服务器端代码:

    var http = require('http');
    var urllib = require('url');
    
    var port = 10011;
    var data = {'name': 'jifeng', 'company': 'taobao'};
    
    http.createServer(function(req, res){
      var params = urllib.parse(req.url, true);
      console.log(params);
      if (params.query && params.query.callback) {
        //console.log(params.query.callback);
        var str =  params.query.callback + '(' + JSON.stringify(data) + ')';//jsonp
        res.end(str);
      } else {
        res.end(JSON.stringify(data));//普通的json
      }     
    }).listen(port, function(){
      console.log('server is listening on port ' + port);  
    })

    2. 游览器端代码,为方便起见,我直接用了jquery的方法

    <html>  
    <head>  
      <script src="http://code.jquery.com/jquery-latest.js"></script>  
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    </head>  
    <body>  
    <script type="text/javascript">  
    function get_jsonp() {  
      $.getJSON("http://10.232.36.110:10011?callback=?",  
      function(data) {
        $('#result').val('My name is: ' + data.name);  
      });  
    }  
    </script>  
    <a href="javascript:get_jsonp();">Click me</a><br />  
    <textarea id="result" cols="50" rows="3"></textarea>  
    </body>  
    </html>  

      jquery中getJSON()方法可以参见:http://www.w3school.com.cn/jquery/ajax_getjson.asp

  • 相关阅读:
    P1428 小鱼比可爱
    P5727 【深基5.例3】冰雹猜想
    P1427 小鱼的数字游戏
    P1047 [NOIP2005 普及组] 校门外的树
    P5729 工艺品制作
    P5728 【深基5.例5】旗鼓相当的对手
    CodeSmith使用和语法简介
    系统缓存
    FLV视频转换的利器 ffmpeg.exe
    Vs.Net方向将Excel数据导入到数据库
  • 原文地址:https://www.cnblogs.com/duhuo/p/4548969.html
Copyright © 2011-2022 走看看