简体   繁体   English

Sencha Touch代理的Java语言回调

[英]Javascript callback for Sencha Touch proxy

I don't understand how to get information from another domain in Sencha Touch. 我不明白如何从Sencha Touch中的另一个域获取信息。 I want to fill my store with data retrieved from a javascript, that got the information from another webservice. 我想用从javascript检索到的数据填充我的商店,该数据是从另一个web服务获取的信息。

For a test, I wrote following code in simpleTest.js: 为了进行测试,我在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? 如何在JavaScript中创建回调? Do I need to stringify the returning string to with json? 我是否需要使用json将返回的字符串字符串化?

You need to tell the JavaScript to output the json, you cannot just write it in. Your code should be: 您需要告诉JavaScript输出json,您不能只写它。您的代码应为:

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

You were also missing braces around your json {} 您还缺少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). 您可以使用Ext.JSON类对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. 我个人使用PHP后端,仅使用本机PHP函数吐出json。

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

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