0

用上传时返回的persistentId来查询数据处理的状态。结果报错如下:

jquery-2.0.3.min.js:5 
XMLHttpRequest cannot load http://api.qiniu.com/status/get/prefop?id=z0.5981927345a2650c99932b57. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost' is therefore not allowed access.

下面是请求代码,是在jssdk基础上修改,新增定时器ajax请求查询。

timerId = setInterval(function() {
      console.log('I am checking pfop_status every 5 seconds');

      // statusUrl = 'pfop_status.php?id=' + res.persistentId;
      statusUrl = 'http://api.qiniu.com/status/get/prefop?id=' + res.persistentId;
      statusAnchor = tdProgress.find('.process-status a');

      $.ajax({
          url: statusUrl,
          async: false,
          // dataType: 'jsonp', //改为jsonp也是报错 Uncaught SyntaxError: Unexpected token
      }).done(function(resp) {
          console.log(resp);
          statusObj = JSON && JSON.parse(resp) || $.parseJSON(resp);
          item = statusObj.items[0]

          switch (item.code) {
              case 0:
                  statusAnchor.text('处理成功');
                  processedPlayBtn.show();
                  processedLink += encodeURIComponent(item.key);
                  processedPlayBtn.attr('data-url', processedLink);
                  clearInterval(timerId);
                  return;
              case 1:
                  statusAnchor.text('等待处理');
                  break;
              case 2:
                  statusAnchor.text('正在处理');
                  break;
              case 3:
                  statusAnchor.text('处理失败');
                  clearInterval(timerId);
                  break;
              case 4:
                  statusAnchor.text('通知失败');
                  clearInterval(timerId);
                  break;
          }
      });
  }, 5000); //5 seconds

请问问题出在哪里??

查看全部 4 个回答

1

跨域了,用jsonp
jQuery的话要指定callback

    $.ajax({  
        url:statusUrl,  
        dataType:'jsonp',  
        data:'',  
        jsonp:'callback',  //回调函数
        success:function(result) {  
            //
        }
    }); 

服务端要用回调函数包含起来

$callback=$_GET['callback'];  
echo $callback."($data)";