简体   繁体   中英

Phonegap app communicate with php server without using ip address

Question: How can I get a phonegap application (using ajax) to communicate with a php server inside a local network without knowing the server's IP address?

I tried using the hostname as the URL ( http://hostname/ ) and it works on my computer's browser but it doesn't work on android or in my application.

Right now, the server has a static IP address but I would like for the application to work with a server that has a dynamic IP address instead.

I came across a couple of questions on here that were similar except they required connecting to an external server ( http://jsonip.appspot.com/ ) and they returned the IP address given by the service provider. I am looking for something to run in a local network to give the IP address assigned by the router.

The only other thing I could think of was sending a UDP broadcast which, from what I've read, is hard to do if not impossible.

Would anyone be able to provide some more input or options?

Thanks

EDIT:

$.ajax({
    type: "POST",
    dataType: "JSON",
    url: "http://home-server/application.php",
    data: {username: $("#username").val(), password: $("password").val()}
    success: function(data) {
        if (data == "approved") {
            //redirect user to home page
        } else if (data == "invalid") {
            //clear form
        }
    },
    error: function(x, exception) {
        alert("Error");
    }
});

The url works on my computer but android/phonegap can't resolve the url. I read up on the access element and my understanding of it led me to try the following without luck:

<access origin="http://home-server/" />
<access origin="http://home-server/application.php" />
<access origin="*" />

You don't need to define the IP address, you can use the domain without problems. Is posible you have not access to connect to with the server.

Try add on your config.xml:

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

Or:

<access origin="*" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM