简体   繁体   中英

Javascript callback for Sencha Touch proxy

I don't understand how to get information from another domain in Sencha Touch. I want to fill my store with data retrieved from a javascript, that got the information from another webservice.

For a test, I wrote following code in simpleTest.js:

function hello(){
"test":[{ "text": "hello"}];
}

And the store looks like this:

Ext.define("AccessibleMap.store.Teststore", {
    extend: "Ext.data.Store",
    config: {
        model: "AccessibleMap.model.Testmodel",
        autoLoad: true,
        proxy:{
            type: 'jsonp',
            url: 'http://test.accessiblemap.square7.ch/live%20access/scripts/simpleTest.js',
            reader:{
                type : 'json',
                rootProperty : 'test'
            }
        }
    }
});

The error message shows, that it can't find the page.

Uncaught SyntaxError: Unexpected token :            simpleTest.js:2  

The problem seems to be the callback. How can I fix this?

How can I create a callback in javascript? Do I need to stringify the returning string to with json?

You need to tell the JavaScript to output the json, you cannot just write it in. Your code should be:

function hello(){
    document.write('{"test":[{ "text": "hello"}]}');
}

You were also missing braces around your json {}

Some other things to note as well when you start actually writing your proper callbacks. You can use the Ext.JSON class to encode / decode json (Ext.JSON.encode / Ext.JSON.decode). I personally use a PHP backend and just use the native PHP functions to spit out my json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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