从Sencha touch app向Node.js服务器发送请求

时间:2014-02-25 23:48:38

标签: node.js request sencha-touch

我最近开始使用Sencha Touch进行编码,我想创建一个带有按钮的简单应用程序,该按钮向Node.js服务器发送请求。然后我想显示一条简单的消息,看到服务器收到了请求。

我尝试了Ext.Ajax.request和Ext.data.JsonP.request但没有成功......我搜索了很多并尝试了很多东西。

现在我的代码app端看起来像:

    launch: function() {

    var button = Ext.create('Ext.Button', {
        iconCls: 'action',
        ui: 'action',
        handler: function() {
            Ext.Ajax.request({
                url: 'localhost:8080',
                success: function (response) {
                    Ext.Msg.alert('Success');
                },

                failure: function (response) {
                    Ext.Msg.alert('Failure');
                }
            });
        },
    });

    Ext.Viewport.add(Ext.create('Ext.Container', {
        fullscreen: true,
        layout: {
            type: 'vbox',
            pack: 'center',
            align: 'center'
        },
        items: [
            {
                html: 'Send The Request'
            },
            {
                items: [button]
            }
        ]
    }));
},

,服务器端是:

var http = require('http');
var url = require('url');

var server = http.createServer(function(request, response) {
    var page = url.parse(request.url).pathname;
    console.log(page);
    response.writeHead(200);
});

server.on('request', function(request, response) {
    response.end('42');
});

server.listen(8080);

我知道这些代码是垃圾,放纵我是javascript中的新手。

提前致谢

2 个答案:

答案 0 :(得分:0)

localhost:8080无法在手机上部署应用程序,因为它将针对手机本身(但可以从浏览器进行测试)。 url也应该要求协议(http://)。

答案 1 :(得分:0)

您需要对此进行调试,并确定请求失败的时间点。

您可以从节点尝试console.log,以查看是否收到了请求。或者,在Chrome中打开该应用并使用检查员的网络标签查看是否已发送请求,如果是,则响应是什么(如果有)。