【问题标题】:Access-Control-Allow-Origin error on same domain同一域上的 Access-Control-Allow-Origin 错误
【发布时间】:2013-06-12 17:17:24
【问题描述】:

我的页面现在已经工作了一段时间,突然间它给了我这个跨域请求错误。奇怪的是我正在对文件发出相对请求,所以我不明白为什么会得到这个。

错误:

选项https://www.aliahealthcare.com/php/central.php?logout=true Access-Control-Allow-Origin 不允许来源https://aliahealthcare.com

发送 jquery-1.7.2.min.js:4 f.extend.ajax jquery-1.7.2.min.js:4 注销 aliahealthcare.com/:841 选择器 aliahealthcare.com/:901 (匿名功能)aliahealthcare.com/:713 f.event.dispatch jquery-1.7.2.min.js:3 h.handle.i jquery-1.7.2.min.js:3

XMLHttpRequest 无法加载 https://www.aliahealthcare.com/php/central.php?logout=true

访问控制允许来源不允许来源https://aliahealthcare.com。 aliahealthcare.com/:1

代码:

 function logout (){

      $.ajax({
           type: "GET",
           url: "/php/central.php",
                   data: { logout: 'true' },
                   success: function(msg) {
                     if(msg=='1'){ 

                     location.reload();

                    } 
                    else if(msg=='0') {
                        alert("Logout Failed! Please try again!") 
                    }
                  },
                  error:function(msg){
                    alert("Error"+msg);
                    }

         });
     }

ajax输出结果时的结果:

Object {readyState: 1, setRequestHeader: function, getAllResponseHeaders: function, getResponseHeader: function, overrideMimeType: function…} abort: function (a){a=a||"abort",p&&p.abort(a),w(0,a);return this} always: function (){i.done.apply(i,arguments).fail.apply(i,arguments);return this} complete: function (){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this} done: function (){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this} error: function (){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this} fail: function (){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this} getAllResponseHeaders: function (){return s===2?n:null} getResponseHeader: function (a){var c;if(s===2){if(!o){o={};while(c=bG.exec(n))o[c[1].toLowerCase()]=c[2]}c=o[a.toLowerCase()]}return c===b?null:c} isRejected: function (){return!!i} isResolved: function (){return!!i} overrideMimeType: function (a){s||(d.mimeType=a);return this} pipe: function (a,b,c){return f.Deferred(function(d){f.each({done:[a,"resolve"],fail:[b,"reject"],progress:[c,"notify"]},function(a,b){var c=b[0],e=b[1],g;f.isFunction(c)?i[a](function(){g=c.apply(this,arguments),g&&f.isFunction(g.promise)?g.promise().then(d.resolve,d.reject,d.notify):d[e+"With"](this===i?d:this,[g])}):i[a](d[e])})}).promise()} progress: function (){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this} promise: function (a){if(a==null)a=h;else for(var b in h)a[b]=h[b];return a} readyState: 0 responseText: "" setRequestHeader: function (a,b){if(!s){var c=a.toLowerCase();a=m[c]=m[c]||a,l[a]=b}return this} state: function (){return e} status: 0 statusCode: function (a){if(a){var b;if(s<2)for(b in a)j[b]=[j[b],a[b]];else b=a[v.status],v.then(b,b)}return this} statusText: "error" success: function (){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this} then: function (a,b,c){i.done(a).fail(b).progress(c);return this} __proto__: Object

【问题讨论】:

  • http://blahblah.comhttp://www.blahblah.com 在跨域策略上有所不同。
  • @phpisuber01 没关系,还是相对路径,暗示请求会使用源域。
  • @BradM 同意,但他的 Options/Origin 信息会暗示其他情况。
  • @Osman 你能不能像var jqXhr = $.ajax('your settings here'); 一样,然后发布console.log(jqXHR) 的结果。也许你在$.ajaxSetup()中有一些冲突设置
  • 好的,我会这样做并发布

标签: php jquery


【解决方案1】:

网址之间的区别是“www.”,我认为这足以触发“跨域”。您说得对,相对请求导致不同的 URL 很奇怪;也许这与 JQuery 的 ajax 方法有关。您可以通过手动输入完整的主机名或使用window.location 确定它来快速解决问题

【讨论】:

  • 我不想硬编码名称以允许用户使用具有相同代码的 www 子域。
  • 那你就可以走第二条路了。 window.location.host(或 .hostname)将为您提供 http:// 和下一个斜杠之间的所有内容。在这里,它是“stackoverflow.com”。在 Google 上,它是“www.google.com”。我们经常将它用于我们的 web 应用程序,它通常会在不同的服务器上运行。
猜你喜欢
  • 2018-12-12
  • 1970-01-01
  • 2017-06-15
  • 2013-12-02
  • 2014-03-05
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
  • 2016-01-04
相关资源
最近更新 更多