简体   繁体   English

Extjs 4 - 以json格式检索数据并加载Store。 它发送OPTION请求

[英]Extjs 4 - Retrieve data in json format and load a Store. It sends OPTION request

I'm developing an app with Spring MVC and the view in extjs 4. At this point, i have to create a Grid which shows a list of users. 我正在使用Spring MVC和extjs 4中的视图开发一个应用程序。此时,我必须创建一个显示用户列表的Grid。

In my Spring MVC controller i have a Get method which returns the list of users in a jsonformat with "items" as a root. 在我的Spring MVC控制器中,我有一个Get方法,它以json格式返回用户列表,其中“items”作为根。

 @RequestMapping(method=RequestMethod.GET, value="/getUsers")
 public @ResponseBody Users getUsersInJSON(){
     Users users = new Users();
     users.setItems(userService.getUsers());
     return users;
 }

If i try to access it with the browser i can see the jsondata correctly. 如果我尝试使用浏览器访问它,我可以正确地看到jsondata。

{"items":[{"username":"name1",".....

But my problem is relative to request of the Ext.data.Store 但我的问题是与Ext.data.Store的请求有关

My Script is the following: 我的脚本如下:

Ext.onReady(function(){

    Ext.define('UsersList', {
        extend: 'Ext.data.Model',
        fields: [
            {name:'username', type:'string'},
            {name:'firstname', type:'string'}
        ]
    });

    var store = Ext.create('Ext.data.Store', {
        storeId: 'users',
        model: 'UsersList',
        autoLoad: 'true',
        proxy: {
            type: 'ajax',
            url : 'http://localhost:8080/MyApp/getUsers.html',
            reader: {type: 'json', root: 'items'}
        }
    });



    Ext.create('Ext.grid.Panel',{
        store :store,
        id : 'user',
        title: 'Users',
        columns : [ 
            {header : 'Username',  dataIndex : 'username'}, 
            {header : 'Firstname', dataIndex: 'firstname'}
        ],
        height :300,
        width: 400,
        renderTo:'center'
    });
});

When the store tries to retrieve the data and launchs the http request, in my firebug console appears OPTIONS getUsers.html while the request in the browser launchs GET getUsers.html 当商店尝试检索数据并启动http请求时,在我的firebug控制台中出现OPTIONS getUsers.html,而浏览器中的请求启动GET getUsers.html

As a result, Ext.data.Store has not elements and the grid appears with the columnames but without data. 因此,Ext.data.Store没有元素,网格显示列名但没有数据。 Maybe i've missed something 也许我错过了什么

Thank you 谢谢

You can change the HTTP methods that are used by the proxy for the different CRUD operations using actionMethods . 您可以使用actionMethods更改代理用于不同CRUD操作的HTTP方法。

But, as you can see in the doc (and as should obviously be the case), GET is the default for read operations. 但是,正如您在doc中看到的那样(显然应该是这种情况),GET是读取操作的默认设置。 So the OPTIONS request you are observing is quite puzzling. 所以您观察的OPTIONS请求非常令人费解。 Are you sure that there's not another part of your code that overrides the default application-wide? 您确定代码的其他部分没有覆盖默认的应用程序范围吗? Maybe do a search for 'OPTIONS' in all your project's JS files, to try and find a possible suspect. 也许在你所有项目的JS文件中搜索'OPTIONS',试图找到可能的嫌疑人。 Apparently there's no match in the whole Ext code, so that probably doesn't come from the framework. 显然,在整个Ext代码中没有匹配,所以这可能不是来自框架。

Edit: 编辑:

Ok, I think I've got it. 好的,我想我已经知道了。 If your page is not accessed from the same domain (ie localhost:8080, the port is taken into account), the XHR object seems to resort to an OPTIONS request. 如果不从同一域访问您的页面(即localhost:8080,考虑端口),则XHR对象似乎采用OPTIONS请求。

So, to fix your problem, either omit the domain name completely, using: 因此,要解决您的问题,请完全省略域名,使用:

url: '/MyApp/getUsers.html'

Or double check that your using the same domain and port to access the page and make the requests. 或者仔细检查您是否使用相同的域和端口来访问该页面并发出请求。

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

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