phonegap javascript html获取请求

fre*_*red 6 javascript phonegap

我正在尝试通过电话间隙应用程序向我的网站发送http get请求.以下代码使用chrome在我的笔记本电脑上完美运行,但一旦转换为应用程序它就不再有效.特别是,phonegap编译代码很好,并给我一个完美的.apk文件,但只有文本"qwer"被放到屏幕上.这是代码:

<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>
Run Code Online (Sandbox Code Playgroud)

请注意,当您执行get请求时,我的网站当前返回"asdf"

我在chrome中运行它时得到的输出是这样的:

asdf qwer
Run Code Online (Sandbox Code Playgroud)

但当它作为Android应用程序运行时,我得到这个:

qwer
Run Code Online (Sandbox Code Playgroud)

另外我想提一下,我已经阅读了所有这些,但没有一个特别有帮助:

在手机间隙运行https请求
https请求在phonegap-iphone应用程序中不起作用 想在iphone上的phonegap应用程序中使用https请求

当请求来自应用程序时,get请求似乎返回一个空字符串,因为没有抛出任何错误,并且它允许我将字符串添加到一起.

我想知道如何解决这个问题,以便我在应用程序表单中工作.我怀疑有某种内置的phonegap功能来做这种事情.

谢谢

编辑:

我在服务器端处理过CORS.我在heroku上使用python.这是我的代码:

#!/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))
Run Code Online (Sandbox Code Playgroud)

如果我需要做其他事情来解决它,请告诉我

小智 4

我不确定问题是什么,但我认为这可能与 CORS(跨源资源共享)有关。

您必须设置 Access-Control-Allow-Origin

您的浏览器正在发出请求并且工作正常,但是当涉及到移动设备时,您必须设置一些额外的参数

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

这个链接可能有用,也许不是java的东西,而是Access-Control-Allow-Origin的设置。看一看。