zoukankan      html  css  js  c++  java
  • ajax jsonp的跨域请求

    1.页面ajax的请求

    $.ajax({
      async: false,
      url: 'http://localhost:8080/downloadVideos',//跨域的dns/document!searchJSONResult.action,
      type: "GET",
      dataType: 'jsonp',
      jsonp: 'jsoncallback',
      jsonpCallback: "cb",//自定义返回函数的名称,要与服务端保持一致
      data: {
        id:ID,
        user:user
      },
      timeout: 5000,
      success: function (json) {
        $(data).css("background","red");
        alert(json);
      }
    });

    2.服务端nodeJS的返回

    app.get('/downloadVideos', function (req, res, next) {
      var random = new Date().getTime();
      var url = "https://savemedia.com/generate/";
      var videosID = req.query.id;
      superagent
        .get(url + videosID + "?random=" + random)
        .end(function (err, sres) { // callback
          // 常规的错误处理
          if (err) {
            return next(err);
          }
          var obj = JSON.parse(sres.text);
          var url = obj.download.watch[0].url;
          var videoName = new Date().getTime();
     
          downloadFile(url, "../public/videos/" + videoName + ".mp4", function (err) {
          if (err) {
            return next(err);
          // console.log(videoName + ".mp4" + '下载完毕');
          });
          var videoUrl = 'http://**.**.**.**:8080/videos/' + videoName + '.mp4';
          res.send("cb(" + JSON.stringify(videoUrl) + ")");
        });
     });

    //文件下载
    function downloadFile(uri, filename, callback) {   var stream = fs.createWriteStream(filename);   request(uri).pipe(stream).on('close', callback); }
  • 相关阅读:
    Android Developers:使ListView滑动流畅
    Nexus 搭建maven 私有仓库
    Eclipse 配置Maven以及修改默认Repository
    maven常用命令介绍
    maven 相关
    session机制详解以及session的相关应用
    正确理解web交互中的cookie与session
    前端开发中Cookie那些事儿
    html转义表
    常用的Linux命令
  • 原文地址:https://www.cnblogs.com/yysbolg/p/7429790.html
Copyright © 2011-2022 走看看