Search code examples
titaniumappcelerator

unable to send request from emulater to WCF


I am coding a login page with Titanium framework and using Android simulator. I need to authenticate user. I am using below mentioned code, but i get "Error" alert.

var loginWindow = Titanium.UI.createWindow({
            backgroundColor: '#CCD0D3',
            title: L('Login'),
            fullscreen: false,
            activity : {
                onCreateOptionsMenu : function(e) {
                    var menu = e.menu;
                    var cancel = menu.add({ title : L('Cancel') });
                    cancel.addEventListener('click', function(e) {
                        var alertDialog = Titanium.UI.createAlertDialog({
                            title: 'Clicked',
                            message: 'Cancel was clicked',
                            buttonNames: ['OK']
                        });
                        alertDialog.show();
                    });

                    var login = menu.add({ title : L('Login') });

                    login.addEventListener('click',function(e)
                    {
                        alert("Click");
                        if (txtUsername.value != '' && txtPassword.value != '')
                        {
                            var loginReq = Titanium.Network.createHTTPClient();
                            loginReq.onreadystatechange = function(){
                                alert('onreadystatechange');
                            };
                            loginReq.onload = function()
                            {
                                alert("load");
                                var json = this.responseText;
                                var response = JSON.parse(json);
                                if (response.logged == true)
                                {
                                    alert("Welcome " + response.name + ". Your email is: " + response.email);
                                }
                                else
                                {
                                    alert(response.message);
                                }
                            };
                            loginReq.onerror = function(){
                                alert("Error");
                            };
                            loginReq.open("GET","http://localhost/iMessage/Authenticate.svc/CheckLogin/praveen/matoria");
                            // var params = {
                                // username: txtUsername.value,
                                // password: Ti.Utils.md5HexDigest(txtPassword.value)
                            // };
                            //loginReq.send(params);


                            loginReq.send(null);
                        }
                        else
                        {
                            alert("Username/Password are required");
                        }
                    });
                }
            },
            exitOnClose:true
        });

If i use browser and send the same request than it sends me following response:

{"email":"[email protected]","logged":true,"name":"Praveen Matoria"}

FYI: For reference i am using following link:

Thanks in advance.


Solution

  • I found the way finally to use localhost for android. it clicked to my brains when i was accessing my virtual machine.

    Solution: Every machine has atleast 3 IP addresses:

    1. localhost
    2. 127.0.0.1
    3. Some internet IP addres through which you access internet. Go to command prompt and type "ipconfig". In my case, 192.168.1.6 was the IP address.

    Use this and your requests would go to router first and than come back and forth.

    May be i am wrong at some facts, but overall idea is same.