加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax jsonp的跨域请求

发布时间:2020-12-16 03:20:15 所属栏目:百科 来源:网络整理
导读:1.页面ajax的请求 $.ajax({ async: false , url: 'http://localhost:8080/downloadVideos', // 跨域的dns/document!searchJSONResult.action, type: "GET" , dataType: 'jsonp' , jsonp: 'jsoncallback' , jsonpCallback: "cb", // 自定义返回函数的名称,要

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); }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读