简体   繁体   English

ExtJS在跨域失败时发送Ext.Ajax.request

[英]ExtJS Send Ext.Ajax.request on cross-domain failure

Hi I know this is an old issue. 嗨,我知道这是一个老问题。

I refer to this but still doesn't work. 我指的是这个,但仍然行不通。

Chrome show this error message: Chrome显示此错误消息:

XMLHttpRequest cannot load target url . XMLHttpRequest无法加载目标url No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' my web url ' is therefore not allowed access. 因此,不允许访问来源“ 我的Web网址 ”。

Here is my code: 这是我的代码:

Ext.Ajax.request({
        url: 'target url',
        method: 'POST',
        cors: true,
        useDefaultXhrHeader: false,
        success: function() {
            alert('success');
        },
        failure: function() {
            alert('failure');
        }
    });

The key to the solution is hidden in plain view in the first line of the post you refered to; 解决方案的关键隐藏在您所引用帖子的第一行中的纯视图中; let me cite it: 让我引用一下:

Backend sends Access-Control-Allow-Origin: * correctly 后端正确发送Access-Control-Allow-Origin: *

Well, your browser states that your backend doesn't do that: 好吧,您的浏览器指出您的后端没有这样做:

No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“ Access-Control-Allow-Origin”标头

The issue is that the browser does not allow your code to access anything it wants. 问题是浏览器不允许您的代码访问所需的任何内容。 The server has to actively allow your page to read the json data. 服务器必须主动允许您的页面读取json数据。 The server can indicate this by setting the Access-Control-Allow-Origin header accordingly (eg to target url if it wants to allow your site specifically, or * as a wildcard). 服务器可以通过相应地设置Access-Control-Allow-Origin标头来表明这一点(例如,如果要专门允许您的站点, target urltarget url ,或者以*作为通配符)。

Shouldn't you be able to find a solution how to add such header in your backend by yourself, you may create a new question containing details about the backend you use. 您是否不应该找到一种解决方案,如何在自己的后端中添加此类标头,您可能会创建一个新问题,其中包含有关所用后端的详细信息。

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

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