2

我想使用 Sencha Touch 登录到外部站点(不是我自己的)。

这是我的一些代码:

登录控制器

Ext.define('Myapp.controller.Login', {
extend: 'Ext.app.Controller',

config: {
    refs: {
       loginForm: '#login-form'  
    },
    control: {
       '#login-button': {
          tap: 'onLoginHandler'
       } 
    }
},

onLoginHandler: function() {
  var form = this.getLoginForm();
  Ext.Ajax.request({
      url: 'https://www.site.com/login.html',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded'},
      method: 'POST',
      params: form.getValues(),
      callback: function(options, success, response){
        console.log(options);
        console.log(response);
      }
  });
}
});

登录视图

Ext.define('Myapp.view.Login', {
    extend: 'Ext.form.Panel',
    alias:  'widget.loginview',
    xtype: 'loginform',
    id: 'login-form',
    requires: [
        'Ext.form.FieldSet'
    ],

    config: {
        title: 'Log in',
        iconCls: 'user',
        html: 'Login View',
        method: 'POST',
        items: [ 
            {
                xtype: 'fieldset',
                title: 'Log in',
                items: [ 
                    bunch of fields
                ]
            },
        {
            xtype: 'button',
            text: 'Login',
            ui: 'action',
            id: 'login-button'

        } 
    ]
}
});

我在浏览器调试器中看到的结果不是我所期望的:

Request URL:https://www.site.com/login.html?_dc=1375705385394
Request Headers
    Access-Control-Request-Headers:origin, x-requested-with, content-type
    Access-Control-Request-Method:POST
    Origin:http://localhost
    Referer:http://localhost/~me/non/
    User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
Query String Parameters URL encoded
    _dc:1375705385394

表单数据甚至不存在。但它显示在来自“console.log(response)”的console.log中(参见控制器),所以它应该被发送

节点的请求模块似乎按我的意愿工作..

var request = require('request');
var form = require('form-data');
var r = request.post('https://www.site.com/login.html', function(err, resp, body){

});

var frm = r.form();
frm.append(...// same fields as in my View, values are hardcoded for testing

这两者之间的区别在于表单是在请求之后附加的......?

这是我手动登录时的标题:

Request URL:https://www.site.com/login.html
Request Method:POST
Status Code:302 Found
Request Headers
    Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/ *;q=0.8
    Accept-Encoding:gzip,deflate,sdch
    Accept-Language:en-US,en;q=0.8,sv;q=0.6
    Cache-Control:max-age=0
    Connection:keep-alive
    Content-Length:53
    Content-Type:application/x-www-form-urlencoded
    Cookie:NOW=4b21bf97818dd91f28732aec2b12344e28ac4aef; TUX-COOKIE=R2414511234; /=; /firsttimeload=0; s_cc=true; s_sq=%5B%5BB%5D%5D
    Host:www.site.com
    Origin:https://www.site.com
    Referer:https://www.site.com/
    User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
Form Data URL encoded
    ...my secret form data shows up here
Response Headersview source
    Connection:Keep-Alive
    Content-Encoding:gzip
    Content-Length:184
    Content-Type:text/html; charset=iso-8859-1
    Date:Mon, 05 Aug 2013 12:57:09 GMT
    Keep-Alive:timeout=5, max=100
    Location:../redirect/location
    Server:Apache
    Set-Cookie:TUX-COOKIE=R2414511234; path=/
    Set-Cookie:NOW=none; path=/; expires=Fri, 08-Aug-2003 12:57:09 GMT; secure
    Set-Cookie:NOW=e555573f3e21a1f008f4be34c7c61234b1014b6; path=/; secure
    Vary:Accept-Encoding,User-Agent

欢迎任何想法或想法!谢谢!

4

1 回答 1

0

通常我会如何处理这个问题是使用 NodeJS 或 PHP 在我的本地服务器上创建一个代理服务。这个过程将允许我将我的 ajax 请求提交到同源,然后我将使用后端 post/fetch 与第三方服务进行交互。

除了通过遵守同源政策提供的额外安全性之外;这有几个额外的好处。您的 Ext 应用程序可以做更少的手工争吵,并且您可以在您的代理中支持多个服务端点。如果服务发生更改,您将拥有可以在没有 UI 的情况下进行测试的服务器代码,这可以简化调试。

可能可以使用 JSONP 提交您的请求,但如果您正在传输登录凭据,那将是非常不明智的做法。

于 2013-08-05T20:31:49.830 回答