phonegap javascript html get requests



我正在尝试从电话间隙应用程序向我的网站发出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>

请注意,当您执行 GET 请求时,我的网站当前返回"ASDF">

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

asdf qwer

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

qwer

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

在Phone Gap中运行HTTPS请求
HTTPS请求在PhoneGap-iPhone应用程序中不起作用 想要在iPhone上的Phonegap应用程序中使用https请求

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

我想知道如何解决此问题,以便在应用程序表单中工作。我怀疑有某种内置的电话间隙功能可以做这种事情。

谢谢

编辑:

我已经在服务器端处理过 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))

如果还有其他事情需要解决,请告诉我

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

你必须设置 访问控制允许源

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

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

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

检查您的配置.xml您确实在导入whitelist-plugin并允许 http 和 https URL。

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

最新更新