【发布时间】:2017-05-20 02:46:59
【问题描述】:
我正在尝试从我的 cordova 应用程序发出 POST http 请求,但它不工作(ios 或 android),我一直收到 httpRequest.status 等于 0 并且 httpRequest.responseText 为空,并且未到达服务器.
有很多关于这方面的信息,但我无法让它发挥作用。我的 config.xml 配置如下:
<access origin="*">
我也尝试对 google 或其他已知服务器进行 GET 查询,但也无法正常工作。
这是我正在使用的代码。
function alertContents(httpRequest)
{
if (httpRequest.readyState == 4)
{
// Everything is good, the response is received
if ((httpRequest.status == 200) || (httpRequest.status == 0))
{
alert("The response was: " + httpRequest.status + " - " + httpRequest.responseText);
} else {
alert('There was a problem with the request. ' + httpRequest.status + httpRequest.responseText);
}
}
}
function send_with_ajax() {
alert("Pushing data");
var the_url = 'http://<servier ip>:<port>/service';
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
httpRequest.open("POST", the_url, true);
httpRequest.send(null);
}
根据网上的信息,它应该可以工作,有人知道我是否遗漏了什么吗?,
非常感谢。
【问题讨论】:
-
send_with_ajax怎么叫? -
一旦收到这样的事件“deviceready”就会调用它: var buttonElement = document.getElementById('data_button'); buttonElement.addEventListener("click", send_with_ajax, false);
-
你的问题。你没有取消点击事件...
标签: javascript android iphone cordova