【问题标题】:Sencha touch 2 cannot get Ajax calls workingSencha touch 2 无法让 Ajax 调用正常工作
【发布时间】:2014-03-16 14:29:03
【问题描述】:

我有这个简单的函数,我从我的应用程序中调用 launch() 函数:

function ajaxCall(){
  Ext.Ajax.request({
    url: 'ajax/Hello!', // Request should go to host/ajax url as http get request
                 // and server should return plain text `Hello!` as answer
    success: function(response){
      prompt('Ajax call success!', response.responseText); // My own prompting method
    }
  });
}

问题是这个请求似乎没有被提出。它没有显示在我的 Play 框架服务器或 Google Chrome 的网络开发人员标签中。

编辑 程序似乎也卡在Ext.Ajax.request 函数上。

【问题讨论】:

  • 尝试在 app.js 的 'require' 属性中添加“Ext.Ajax”。
  • 你是在launch里面调用ajaxCall()吗?
  • 是的,我从我的 launch() 方法中调用该方法。
  • 你的sencha在你服务器的同一个域上吗? sencha ajax 无法运行或跨域(需要使用 jsonp)。
  • 这很奇怪。你的提示叫什么?你能在 chrome 网络上查看申请吗?

标签: javascript ajax http sencha-touch sencha-touch-2


【解决方案1】:

下面的 ajax 调用代码对我来说效果很好。

Ext.Ajax.request({
    async : true,
    url : 'api/login/',
    method : 'POST',
    jsonData : {
        "email":Ext.getCmp('loginUserNameTxtBoxId')._value,
        "pwd":Ext.getCmp('loginPasswordTxtBoxId')._value,
    },
    success : function (request, resp) {
        alert("in login success");
    },
    failure: function(request, resp) {
        alert("in failure");
    }
});

【讨论】:

    【解决方案2】:

    您的网址应该是一个 http 网址。例如,您还应该指定操作(GET、POST)。您已经指定要使用 Ext.Ajax.request 进行 ajax 调用。

    如果要使用参数,请使用参数对象。

    示例:

     Ext.Ajax.request({
            url : 'http://google.com/api/blabla',
            method: 'GET',
            params: {
                username: 'bla',
                password: 'blabla'
            },
            success: function(response, request) { console.log('success'); }
        });  
    

    如果已发出请求,请检查 chrome 开发者工具中的网络选项卡。

    【讨论】:

    • Sencha touch 2 Ext.Ajax 文档说我不必添加完整的 url 来拨打电话。 '/ajax' 应该可以正常工作。如果我错了,请纠正我
    • 你是对的。我刚刚检查了文档。如果未指定,则请求使用 GET 方法。但是你在什么上执行 get 方法?主机/ajax?您的服务器在 /ajax 上返回什么?
    • 服务器返回路径 /ajax/something 的纯文本 ok 答案。如果我在浏览器中输入localhost:9000/ajax/Hello!,它会返回纯文本Hello! 作为答案。
    • 我还可以从 chrome 开发人员选项卡中看到,我的请求永远不会像我在问题中指定的那样被提出。
    【解决方案3】:

    我注意到我忘记将Ext.Ajax 添加到我的应用程序需要字段中。添加后一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2012-02-20
      • 2012-05-01
      • 1970-01-01
      相关资源
      最近更新 更多