Mathew
Mathew

Reputation: 1430

phonegap javascript html get requests

I am trying to do a http get request to my website from a phone-gap app. The following code works perfectly fine on my laptop using chrome but once converted to an app it no longer works. In particular, phonegap compiles the code fine and gives me a perfectly fine .apk file but only the text "qwer" is put to the screen. Here is the code:

<p id="output"></p>

<script>

    var theUrl = "https://example.com"

    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            document.getElementById("output").innerHTML = xmlHttp.responseText + "qwer"
    }
    xmlHttp.open("GET", theUrl, true); // true for asynchronous
    xmlHttp.send(null);


</script>

note that my site currently returns "asdf" when you do a get request

The output that I get when I run it in chrome is like so:

asdf qwer

but when it is run as an android app I get this:

qwer

Also I would like to mention I have read all of these and none of them have been particularly helpful:

run https request in phone gap
https request doesn't work in phonegap-iphone app Want to use https request in phonegap application on iphone

It appears that the get request returns an empty string when the request comes from the app seeing as that no error is thrown and that it permits me to add strings together.

I would like to know how I could fix this so that I works in the app form. I suspect there is some kind of built in phonegap function for doing this sort of thing.

Thanks

Edit:

I have dealt with CORS on the server side. I am using python on heroku. Here is my code:

#!/usr/bin/env python
import gevent.monkey
gevent.monkey.patch_all()
import bottle
import os
@bottle.route('/')

def index():
    bottle.response.set_header("Content-Type", "text/turtle")
    bottle.response.set_header("Content-Location", "mydata.ttl")
    bottle.response.set_header("Access-Control-Allow-Origin", "*")
    return("asdf")

bottle.run(server='gevent', host='0.0.0.0', port=os.environ.get('PORT', 5000))

If there is something else I need to do to fix it let me know

Upvotes: 6

Views: 264

Answers (2)

Jose Rojas
Jose Rojas

Reputation: 3520

Check into your config.xml that you are really importing the whitelist-plugin and allow the http and https URL.

<!-- Allow links to web pages to open in a browser -->
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />

Upvotes: 0

Galib Imtiaz
Galib Imtiaz

Reputation: 106

I am not sure what is the problem but I think it might be something related to CORS (Cross-Origin Resource Sharing).

You have to set Access-Control-Allow-Origin

Your browser is making a request and its working fine but when it comes to mobile you have to set some extra parameter

https://www.telerik.com/blogs/using-existing-backend-services-in-phonegap-cordova-applications

this link might be useful , maybe not the java things but the settings of Access-Control-Allow-Origin. Have a look.

Upvotes: 4

Related Questions