1

I'm having problems connecting to my server to check credentials for my phone application, in which I'm using phonegap to develop it.

I've already checked out these questions, but they're not giving me what I'm looking for

HTTP Requests in Phonegap

HTTP methods in PhoneGap

I've read up on the subject and have made multiple sites using get requests, so I have a pretty good understanding on the matter, I'm just not sure what's going on.

http://simonmacdonald.blogspot.com/2011/12/on-third-day-of-phonegapping-getting.html

Here's what my code looks like:

login.onclick = function() {
  var req = new XMLHttpRequest(); 
  req.open('GET', 'myServer'+username.value+".json", true);
  req.send();

  req.onreadystatechange = function() {
      if (req.readyState == 4) {
          if( (req.status == 200) || (req.status == 0) ) {
              var json = JSON.parse(req.responseText);
              if(json.length > 0) {
                  if((json[0]["username"] == username.value) && (json[0]["phoneCred"] == password.value)){
                              localStorage.setItem("loggedIn",username.value);
                              location.reload();
                  }
                  else {
                      alert("Incorrect password");
                  }
              }
              else {
                  alert("Incorrect username");
              }
          }
          else {
              alert("Error talking to server");
          }
      }
  }
};

I added the req.status == 0 into the one if, and now my button is completely unresponsive, I was at least getting the alert that I wasn't talking to the server. If anyone has any insight, it would be much appreciated!

okay so i had a couple of errors in my code, in which i fixed so here's my updated code:

login.onclick = function() {
var req = new XMLHttpRequest();
req.open('GET', 'myServer'+username.value+'.json', true);
req.send();

alert('myServer'+username.value+'.json');
req.onreadystatechange = function() {
    if (req.readyState == 4) {
        if( (req.status == 200) || (req.status == 0) ) {

            //alert(req.responseText.length);
            alert(req.responseText);
            if(req.responseText.length > 0) {
                var json = JSON.parse(req.responseText); 
                if( (json[0].username == username.value) && (json[0].phoneCred == password.value) ){
                            localStorage.setItem("loggedIn",username.value);
                            location.reload();
                }
                else {
                    alert("Incorrect password");
                }
            }
            else {
                alert("Incorrect username");
            }
        }
        else {
            alert("Error talking to server");
        }
    }
}
};

i have two phone on which i've been testing on, and it worked on my galaxy note, but not on my galaxy s4 if that might be a hint towards anything.

i also changed my access tag in my xml file to

<access origin=".*" />

after i did some reading from

Phonegap + Android status=0 returned from webservice

phonegap XMLHttpRequest responseText empty with android but contain data in iphone

and

Empty responseText from XMLHttpRequest

but my galaxy s4 still doesnt seem to receive any data from the server. It may be the same origin policy or some setting for internet access that I'm unsure about. If anyone has any suggestions, I would be very appreciative.

Community
  • 1
  • 1
nyduss
  • 137
  • 2
  • 13

1 Answers1

3

Fixed it. I needed to add

<access origin="http://google.com" />

to the config.xml file, where google.com was my own server. Phonegap has some nice documentation now that you can see here

http://docs.phonegap.com/en/3.3.0/guide_appdev_whitelist_index.md.html#Whitelist%20Guide

nyduss
  • 137
  • 2
  • 13