2012-08-06 68 views
2

我需要一个跨域用户登录请求实例,请帮帮我,谢谢! 我的代码煎茶touch2登录例子

Ext.data.JsonP.request({ 
     url: 'http://25.30.2.3:8080/newvbo/applyaction!longin', 
     params: { 
      username:'13881901678', 
      password:'111111', 
     }, 
     success: function(response, opts) {  
      alert('1');  
    }, 
    failure: function(response, opts) { 
      alert('2');  
    } 
}); 

我的问题是,我没有收到服务器返回的值,我错误?

+0

您正在使用什么后台? – Madman 2012-08-06 09:52:24

+0

我想也许你的服务器响应不是jsonp,请检查回调参数。响应应该是这样的your_callback_key({'data':'hello'}) – Hardy 2013-05-07 19:49:01

回答

0

嗨@jesse你可以尝试跨域这样的事情,

Ext.Ajax.request({ 
     url: 'your_url_path', 
     timeout: 90000, 
     params: { 
      withCredentials: true, 
      useDefaultXhrHeader: false, 
      username:'13881901678', 
      password:'111111', 
     }, 
     success: function(response, o) { 
      if(response != '') { 
        alert('1') 
      } 
     }, 
     failure: function(response, o) { 
      alert('2') 
     } 
}) 

对于跨域你需要把withCredentials: trueuseDefaultXhrHeader: false

我希望能帮到你。

+0

感谢您的帮助,现在的问题是在chrome调试中,Uncaught SyntaxError出现:意外令牌: 请参阅Java调试中的返回代码:{“jsonArray”:null,“jsonMap”:null,“jsonReturn”:“true”} 最后我做错了吗? – jesse 2012-08-07 02:50:00

+0

也许吧,但你的回答是'null'你能告诉我你是怎么做的吗? :) – hekomobile 2012-08-07 03:49:03

3

你可以试试这个

Ext.Ajax.request({ 
method:'GET', 
contentType:'application/json; charset=utf-8', 
dataType:'json', 
url:'http://........Login', 
disableCaching: false, 
withCredentials: true, 
useDefaultXhrHeader: false, 
callbackKey: 'callback', 

params: { 
     EmailId:Ext.getCmp('usernametwoway').getValue(), 
     Password:Ext.getCmp('loginpasswordtwoway').getValue(), 
     }, 


success:function(response) 
{ 
     console.log(response); 
     var res = response.responseText;  


     var jsonarr = Ext.decode(response.responseText); 
     console.log(jsonarr); 
      var myres = jsonarr[0].Result; 

      console.log(myres); 
     switch(myres) 
      { 

      case '1': 
      console.log("Registration response is successfully"); 

      Ext.Viewport.setActiveItem({xtype:'passengerdetailstwoway'}); 

      break; 

      case '0': 
       console.log("Failed"); 
       Ext.Msg.alert("Error","Login Failed",Ext.emptyFn); 
       break; 

根据您可以指定交换机情况下,性反应..

0

我想建议你采用不同的方法,因为发送的登录凭据GET参数到远程服务器通过HTTP不是一个好主意,而应该通过HTTPS使用POST方法。现在你会认为JSONP不支持POST,所以我该如何做到这一点,但是如果你打算在手机或iPad上安装你的应用程序,它会在不使用JsonP的情况下从浏览器进行远程调用,这意味着你可以使用普通的AJAX代理做你想做的。检查了这一点:

How to use json proxy to access remote services during development

这意味着这应该工作:

var obj = new Object(); 
obj.userId = username; 
obj.password = password; 
var data = Ext.JSON.encode(obj); 

Ext.Ajax.request({ 
    url : 'https://login_url', 
    method : "POST", 
    headers: { 
     'Content-Type': 'application/json' 
    }, 
    params : data, 
    useDefaultXhrHeader : false, 
    withCredentials: true, 
    success : function(response) { 

    }, 
    failure : function(response) { 

    } 
});