简体   繁体   English

XMLHttpRequest对象回调不会在跨域jquery / Ajax请求中触发

[英]XMLHttpRequest object callback doesn't fire in crossdomain jquery/Ajax request

So I've got this code: 所以我有以下代码:

$.ajax({
    xhr: function() {
        var xhr = new window.XMLHttpRequest();

       xhr.addEventListener("progress", function(evt) {
           if (evt.lengthComputable) {
               var percentComplete = evt.loaded / evt.total;
               console.log(percentComplete);
           }
       }, false);

       return xhr;
    },
  dataType: "jsonp",
  contentType: "text/javascript charset-utf-8",
  url: "http://*******.com/pcm/datamobile.asmx/ObtenerContactos",
  crossDomain: true, 
  timeout:8000000,
  data: {sessionId: 1},
  error : function (xhr, status) {

      },
 success : function() {FirstAjaxReady();} 
});   
}

The function in the xhr: option never fires. xhr:选项中的函数从不触发。 I'm trying to get a progress report on the request. 我正在尝试获取有关该请求的进度报告。 This may have something to do with the fact that it is cross-domain. 这可能与跨域事实有关。 If so, is there a way to have a progress report in a cross-domain Ajax request? 如果是这样,是否有办法在跨域Ajax请求中提供进度报告?

Crossdomain, or Cross-Origin, needs to be enabled on the server-side as well 还需要在服务器端启用跨域或跨域

http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing

And the CORS security does seem to dictate that progress will not be sent: 而且CORS的安全性似乎决定了不会发送进度:

CORS API specifications also need to ensure not to reveal anything until the cross-origin request status is set to preflight complete or success to prevent eg port scanning. CORS API规范还需要确保在跨域请求状态设置为预检完成或成功之前,不透露任何内容,以防止例如端口扫描。

In XMLHttpRequest progress events are dispatched only after the cross-origin request status is set to success. 在XMLHttpRequest中,仅在跨域请求状态设置为成功后才调度进度事件。 Upload progress events are only dispatched once the cross-origin request status is preflight complete. 仅在跨域请求状态预检完成后才调度上载进度事件。

If I am reading that correctly. 如果我没看错的话。

http://www.w3.org/TR/cors/#preflight-request http://www.w3.org/TR/cors/#preflight-request

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM