java - Extjs 和 Spring MVC XMLHttpRequest 无法加载预检响应具有无效的 HTTP 状态代码 403

标签 java spring extjs cors

当我从 sencha 打电话时:

proxy: {
                        type: 'ajax',
                        url: 'http://localhost:8585/academy/api/alumno/list/',
                        reader: {
                             type: 'json',
                             rootProperty: 'alumnos'
                        }
                    },

我在 spring mvc 上收到了:

@Controller
@RequestMapping("/alumno")
public class AlumnoController {

    @Autowired
    AlumnoApi alumnoApi;

    @RequestMapping(value = "list", produces = "application/json")
    @ResponseBody
    public List<DtoAlumno> findAll(){
        return alumnoApi.findAll();
    }

显示以下错误

XMLHttpRequest cannot load http://localhost:8585/academy/api/alumno/list/?_dc=1467319530160. Response for preflight has invalid HTTP status code 403

如果我打开地址:http://localhost:8585/academy/api/alumno/list/?_dc=1467319530160在浏览器中正确显示json文件

我已经激活了插件Toggle CORS,如果我关闭Toggle会显示错误CORS。

最佳答案

我在服务位于同一服务器但数据库不在同一服务器上的服务器上修复了此问题。

设置:

  1. Ubuntu 14 服务器(不在本地主机上)
  2. Tomcat 在端口 8080 上运行(应用程序在 tomcat 外部运行标准端口 80)
  3. 分配给服务器 IP 的域名(root 到达应用程序,:8080 到达 tomcat 服务)
  4. .htaccess 文件来限制对服务的访问。

问题 调用 Extjs.dataStore.read() 会抛出 400 系列错误,其中包含无效 HTTP 的预检错误。 (与上面的OP相同)

解决方案 更新 .htaccess 文件以包含代理重写,并且不要在数据存储的 URL 中使用端口。

对于提供给 Extjs.dataStore.proxy.url 的 URL,浏览器中的基本 URL 必须与您的服务前调用、端口等相匹配

示例:

浏览器中的网址:

http://localhost:5517/MyApp  //(this is your EXTJS app location note the port number)

在 Extjs 中:

Ext.define('MyApp.store.myDataStore', {
    extend: 'Ext.data.Store',

    requires: [
        'MyApp.model.myDataStoreModel',
        'Ext.data.proxy.Ajax',
        'Ext.data.reader.Json'
    ],

    constructor: function(cfg) {
        var me = this;
        cfg = cfg || {};
        me.callParent([Ext.apply({
            storeId: 'myDataStore',
            autoLoad: true,
            model: 'MyApp.model.myDataStoreModel',
            proxy: {
                type: 'ajax',
                url: 'http://localhost:5517/mainservice/subservice?q=test', //URL localhost:<port> needs to match URL in browser (or use domain without ports)
                reader: {
                    type: 'json',
                    rootProperty: function(data) {
                        console.info('data ='); console.info(data); return data; //show raw return 
                    }
                }
            }
        }, cfg)]);
    }
});
//Ext.StoreMgr.get('myDataStore').read(); //to call read event

在 .htaccess 文件中:

Options +FollowSymLnks
RewriteEngine on

RewriteRule ^mainservice/subservice(.*)$ http://localhost:8585/mainservice/subservice$1 [P]

值得注意, 我无法在本地主机中测试它,因为我的 spring 服务附加到我可以通过本地主机访问之外的其他服务器/数据库。

当浏览器的 URL 与代理 spring 中引用的 URL 不匹配时,就会出现上面的错误。

我不知道这是否会对您有帮助,因为您似乎没有使用 .htaccess 文件,但它可以帮助其他遇到类似问题的人,因为我就是这样找到您的帖子的。

关于java - Extjs 和 Spring MVC XMLHttpRequest 无法加载预检响应具有无效的 HTTP 状态代码 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38140317/

相关文章:

java - 如何在 Eclipse 3.7.1 中安装 Birt 报表设计器

java - Spring -JPA "Class is not entity"

javascript - Extjs6 通过函数从配置应用属性

java - Spring数据库事务未在Db中提交数据

java - Spring 休息模板 : Host name 'localhost' does not match the certificate subject provided by the peer

ExtJs 行编辑插入新行错误 : Uncaught TypeError: Cannot read property 'syncContent' of null

sencha-touch - 为 Sencha 项目重用 ExtJs 代码。实际体验?

java - 由 : java. lang.IllegalArgumentException : Not a managed type: & Caused by: org. hibernate.AnnotationException 引起:没有为实体指定标识符:

java - 添加对象时 ContentPane 背景颜色发生变化

java - Stream<String> 转换为 Stream<Character>