无法在网格视图中显示存储中的 Json 数据(使用 Sencha 框架)

Failing in Display the Json data from store in grid view (Using Sencha Framework)

我是 Sencha 新手。我在网络中收到数据响应,但数据未加载到网格中。如果我尝试在代理中使用静态数据,网格加载这些值。请帮我解决这个问题。

要求URL: http://localhost:8080/list?_dc=1506420300660&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback1

Response:

[{"name":"Thanos","email":"thanos@infgauntlet.com","phone":"5555555555"},{"name":"Spider Man","email":"peter.parker@thebugle.net","phone":"2125555555"},{"name":"Daredevil","email":"matt.murdock@nelsonandmurdock.org","phone":"2125555555"},{"name":"The Maker","email":"reed.richards@therealreedrichards.net","phone":"2125555555"},{"name":"Rocket","email":"rocket@starlordstinks.com","phone":"5555555555"},{"name":"Galactus","email":"galactus@worldeater.com","phone":"5555555555"},{"name":"Silver Surfer","email":"norrin.radd@zenn-la.gov","phone":"5555555555"},{"name":"Hulk","email":"bruce.banner@hulkout.org","phone":"2125555555"},{"name":"Squirrel Girl","email":"doreen.green@nannyservices.net","phone":"2125555555"},{"name":"Thor","email":"thor@odinson.gov","phone":"5555555555"}]



Store:
Ext.define('CRUD.store.Personnel', {
    extend: 'Ext.data.Store',

    alias: 'store.personnel',

    fields: ['name', 'email', 'phone'],

    // data: [
    //     { name: 'Jean Luc', email: "jeanluc.picard@enterprise.com", phone: "555-111-1111" },
    //     { name: 'Worf', email: "worf.moghsson@enterprise.com", phone: "555-222-2222" },
    //     { name: 'Deanna', email: "deanna.troi@enterprise.com", phone: "555-333-3333" },
    //     { name: 'Data', email: "mr.data@enterprise.com", phone: "555-444-4444" }
    // ],

    proxy: {
        headers: {
            "Content-Type": "application/json"
        },
        type: 'jsonp', //cross domain calls - json parser
        url: 'http://localhost:8080/list',
        reader: {
            type: 'json',
        },
        listeners: {

            load: function(store, records, success, operation, data) {
                // Ext.each(records, function(rec) {
                //     Ext.Msg.alert("test")
                //     console.log(rec.get('name'));
                // });

                console.log(JSON.stringify(success));
            },
            exception: function(proxy, response, operation) {
                // exception handling 
                console.log(response);
            }
        }
    },
    // proxy: {
    //     type: 'memory',
    //     reader: {
    //         type: 'json',
    //     }
    // },
    autoLoad: true,
});



View: List.js
/**
 * This view is an example list of people.
 */
Ext.define('CRUD.view.main.List', {
    extend: 'Ext.grid.Panel',

    xtype: 'home',

    requires: [
        'CRUD.store.Personnel'
    ],

    title: 'Heroes',

    store: {
        type: 'personnel'
    },

    columns: [
        { text: 'Name', dataIndex: 'name', flex: 1 },
        { text: 'Email', dataIndex: 'email', flex: 1 },
        { text: 'Phone', dataIndex: 'phone', flex: 1 }
    ],

    listeners: {
        select: 'onItemSelected',
    },
});

您正在使用 JSONP 代理,但您的响应是纯粹的 JSON。这行不通,因为两者之间的数据格式不同。您必须切换到 JSON 代理,或者将服务器 return 的答案更改为 JSONP 格式。我建议您切换到 JSON 代理。

为了 JSON 工作 cross-domain,您的服务器必须

  • 接受使用 OPTIONS 方法发送的呼叫 (so-called "pre-flight requests") 和 return 状态 200,
  • 和 return 特殊 header 对 pre-flight 请求的调用以及实际的 POST/GET 调用。

您必须 return 的 header 可以直接从您收到的错误消息中推断出来,如果您不这样做的话 return。例如,

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:2020' is therefore not allowed access.

意味着你必须在服务器响应中添加一个header Access-Control-Allow-Origin: http://localhost:2020