It is currently Wed Jun 19, 2013 2:29 am

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Cannot login using simple javascript
PostPosted: Wed Nov 30, 2011 9:26 am 
Rookie Tonidoid
Rookie Tonidoid
Joined: Wed Nov 30, 2011 8:08 am
Posts: 4

Hi All,

I am a total newbee to Tonido API. I have installed TonidoLite on my Windows 7 pc. Trying to create a simple login page. Following is my JS code. But when I run this i get following error in chrome's JS console.

Quote:
XMLHttpRequest cannot load http://127.0.0.1:10001/core/loginprofile. Origin http://localhost is not allowed by Access-Control-Allow-Origin


attaching my code below.

Code:

var invocation = new XMLHttpRequest();

function login()
    {
        //var url =SERVER_URL+'core/loginprofile';
        var url ='http://127.0.0.1:10001/core/loginprofile';
        var invocationHistoryText;
        var params = 'profile=strider1981&password='+ sha1Hash(document.getElementById('txtPassword').value);
        if(invocation)
        {
            invocation.open('POST', url, true);
            invocation.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          invocation.setRequestHeader("Content-length", params.length);
          invocation.setRequestHeader("Connection", "close");
            invocation.onreadystatechange = handler;
            invocation.send(params);
        }
        else
        {
            alert("invocation is null")
        }
    }
    function handler(evtXHR)
    {
        if (invocation.readyState == 4)
        {
            if (invocation.status == 200)
            {
                alert("response ok");
            }
            else
            {
                alert("Invocation Errors Occured " + invocation.readyState + " and the status is " + invocation.status);
            }
            alert(invocation.responseText);
        }
        else
        {
            dump("currently the application is at" + invocation.readyState);
        }
    }


Does this seem rite?

Please let me know.

Thnx


Offline
 Profile  
 Post subject: Re: Cannot login using simple javascript
PostPosted: Wed Nov 30, 2011 10:06 am 
Admin Tonidoid
Admin Tonidoid
User avatar
Joined: Tue Dec 30, 2008 12:13 am
Posts: 7408
Location: Dallas, USA

Take a look at this:
http://www.codelathe.com/blog/index.php ... -http-api/


Offline
 Profile  
 Post subject: Re: Cannot login using simple javascript
PostPosted: Wed Nov 30, 2011 11:40 am 
Rookie Tonidoid
Rookie Tonidoid
Joined: Wed Nov 30, 2011 8:08 am
Posts: 4

Thnx for quick reply madhan. I have tried the code in the link that you have posted. No luck with that either. I hit the wall with same error. this is what i get in my chrome console.

Quote:
OPTIONS http://127.0.0.1:10001/core/loginprofil ... ab3dfddeb7 403 (OK)

XMLHttpRequest cannot load http://127.0.0.1:10001/core/loginprofil ... ab3dfddeb7. Origin http://localhost is not allowed by Access-Control-Allow-Origin.


Seems like that the issue is with cross origin resource sharing. How should i tackle it in tonido api? Is there any setting i can configure in my IIS or in Tonido Lite? I am working on an aggressive deadline on this. Any help is highly appreciated.

Thnx


Offline
 Profile  
 Post subject: Re: Cannot login using simple javascript
PostPosted: Wed Nov 30, 2011 12:01 pm 
Admin Tonidoid
Admin Tonidoid
User avatar
Joined: Tue Dec 30, 2008 12:13 am
Posts: 7408
Location: Dallas, USA

Yes, unfortunately a browser won't allow you to access a domain different from where the web page originated. So you can't run the UI in ISS and try to talk to Tonido running on a different port.

Possible solutions are.
1. You can serve the webpage via Webshare and that can access the tonido APIs
2. You can write a server side wrappers on your IIS that you can call from javascript which in turn calls Tonido via HTTP


Offline
 Profile  
 Post subject: Re: Cannot login using simple javascript
PostPosted: Wed Nov 30, 2011 12:39 pm 
Rookie Tonidoid
Rookie Tonidoid
Joined: Wed Nov 30, 2011 8:08 am
Posts: 4

Thnx again for the quick reply.

I am not sure abt webshare(Not really familiar with it). I hv tried ur second approach though.

I have created an asp.net page "Server.aspx" where i am sending all my request which in turn calls Tonido APIs. I got lucky only with loginprofile method. All the other API calls dont seem to work.

Response that i get after calling loginprofile from my asp page is

Quote:
<commands><command><type>loginprofile</type><result>1</result><message></message></command></commands>


But when i call getauthurl method rite after tht, wht i get is following

Quote:
<commands><command><type>AuthenticationFailed</type><result>0</result><message>Bad Role</message></command></commands>


Attaching my JS and ASP code

JS Code
Code:
 function logonASP(userName,pwd)
    {
        $.post('Server.aspx', { 'apiMethod':'login','uid': userName, 'pwd': pwd }, function(data)
        {   
            alert(data);
            $.post('Server.aspx', { 'apiMethod':'getAuthURL'}, function(data)
            {
                alert("data");
            });
        });
    }


ASP.Net code
Code:
Imports System.Net
Imports System.IO

Partial Class Server
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strAPIMethod As String
        strAPIMethod = Request.Form("apiMethod").ToString()

        Select Case strAPIMethod
            Case "login"
                Login()
            Case "getAuthURL"
                GetAuthURL()
        End Select
       
    End Sub

    Private Sub GetAuthURL()
        Dim strResponse As String
        strResponse = SendWebRequest("http://127.0.0.1:10001/core/getauthurl", "POST", "")
        Response.Write(strResponse)
        Response.End()
    End Sub

    Private Sub Login()
        Dim strURL As String
        Dim strUID As String
        Dim strPWD As String
        Dim strResponse As String

        strUID = Request.Form("uid").ToString()
        strPWD = Request.Form("pwd").ToString()
        strURL = "http://127.0.0.1:10001/core/loginprofile"

        strResponse = SendWebRequest("http://127.0.0.1:10001/core/loginprofile", "POST", "profile=" & strUID & "&password=" & strPWD)
        Response.Write(strResponse)
        Response.End()
    End Sub

    Function SendWebRequest(ByVal URL As String, ByVal method As String, ByVal POSTdata As String) As String
        Dim responseData As String = ""
        Try
            Dim objCookies As New CookieContainer
            Dim hwrequest As Net.HttpWebRequest = Net.WebRequest.Create(URL)
            hwrequest.Accept = "*/*"
            hwrequest.AllowAutoRedirect = True
            hwrequest.UserAgent = "http_requester/0.1"
            hwrequest.Timeout = 60000
            hwrequest.Method = method
            hwrequest.CookieContainer = objCookies
            If hwrequest.Method = "POST" Then
                hwrequest.ContentType = "application/x-www-form-urlencoded"
                Dim encoding As New Text.ASCIIEncoding() 'Use UTF8Encoding for XML requests
                Dim postByteArray() As Byte = encoding.GetBytes(POSTdata)
                hwrequest.ContentLength = postByteArray.Length
                Dim postStream As System.IO.Stream = hwrequest.GetRequestStream()
                postStream.Write(postByteArray, 0, postByteArray.Length)
                postStream.Close()
            End If
            Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()
            If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
                Dim responseStream As System.IO.StreamReader = _
                  New System.IO.StreamReader(hwresponse.GetResponseStream())
                responseData = responseStream.ReadToEnd()
            End If
            hwresponse.Close()
        Catch e As Exception
            responseData = "An error occurred: " & e.Message
        End Try
        Return responseData
    End Function
End Class


Any thoughts?


Thnx a lot


Offline
 Profile  
 Post subject: Re: Cannot login using simple javascript
PostPosted: Wed Nov 30, 2011 3:03 pm 
Rookie Tonidoid
Rookie Tonidoid
Joined: Wed Nov 30, 2011 8:08 am
Posts: 4

Got it! I dint check respose cookies. Silly me :D

Thnx for the help anyways


Offline
 Profile  
 Post subject: Re: Cannot login using simple javascript
PostPosted: Thu Dec 01, 2011 6:39 pm 
Admin Tonidoid
Admin Tonidoid
Joined: Fri Dec 26, 2008 10:45 pm
Posts: 152

May we know what you are working on? Just curious.


Offline
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours [ DST ]


 Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: