0

I want to send lonin request with user name and password to Funambol Server which has public ULR (not in localhost). I already have ID and password, and also i can login with my ID in Server, but when i want to use JavaScript to send the login request it dose not response me anything, my question is: Is there any special way to send the request and receive the returned data from Server? I tried the code below, but i did not get any result (the status result which I get: http.readyState = 4 and http.status = 0). If someone knows about this, please give suggestion how to send request and retrieve the data from response?

var http = false;
    var user = "myid";
    var pass = "mypassword";
    if (navigator.appName == "Microsoft Internet Explorer") {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        http = new XMLHttpRequest();
    }
    function handle() {
        if (http.readyState == 4) {
                    var response = http.responseText;
                    //var response = eval('(' + http.responseText + ')');
                    //+"--"+ http.readyState +"--"+http.status;
            document.getElementById('p').innerHTML = "response: " + response.data;
            //countcontacts();
        }
    }
    function login() {
        http.onreadystatechange = handle;
        http.open("POST", "/123.125.221.15/login?action=login&login="+user
        +"&password="+pass, true);
        http.send(null);
    }
    function countcontacts() {
        http.onreadystatechange = function() {
            if (http.readyState == 4 && http.status == 200) {
                the_object = eval('(' + http.responseText + ')');
                document.getElementById('p').innerHTML = "Found "
                        + the_object.count + " contacts.";
            }
        }
        http.open("POST", "/123.125.221.15/contact?action=count", true);
        http.send(null);
    }

And also why var response = eval('(' + http.responseText + ')'); throws this Exception:

uncaught syntaxerror unexpected token ) at file android_asset www index html

Thanks in advance!!

Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
  • I highly recommend using [jQuery ajax](http://api.jquery.com/jQuery.ajax/) rather than raw javascript xhr objects. – Cameron Askew Oct 24 '13 at 07:58
  • Also, you might run into issues with the [Same-origin policy](http://en.wikipedia.org/wiki/Same-origin_policy) here since you're trying to make XHR requests to a site that the javascript file did not originate from. – Cameron Askew Oct 24 '13 at 08:02
  • Thank you! I have tried the code below, also doesn't work, is there any problem with the code below? `$(document).ready(function() {$("#connect").click(function() {$.get("http://www.123.125.221.15:80/login/", {action:"login", login:user, password:pass}).done(function(data, status, xhr) {if(status == "success" && xhr.status == 200) {var response = data;$("#p").text("response: " + response);}}).fail(function() {$("#p").text("Error");});});});` URL I have tried many type like http://www.123.125.221.15:80/login/ and 123.125.221.15:80/login/ and 123.125.221.15/login/.No any result. – Bahramdun Adil Oct 24 '13 at 12:57

0 Answers0