繁体   English   中英

简单的kony.net.httpRequest无法正常工作

[英]Simple kony.net.httpRequest not working

我无法使用下面的kony.net.httpRequest协议连接到任何服务器。 我总是收到“服务器错误”的statusText。

function test(){
var xmlhttp = new kony.net.HttpRequest();
var url = "http://httpbin.org/get";
xmlhttp.open(constants.HTTP_METHOD_GET, url);
xmlhttp.send();
xmlhttp.onReadyStateChange = function(){ sunsetCallback(xmlhttp) };
return;
}
function sunsetCallback(xmlhttp){
alert(xmlhttp.statusText);
if (xmlhttp.responseType==constants.HTTP_RESPONSE_TYPE_DOCUMENT){
    sunrise.lblSunrise.text=xmlhttp.response;
}
sunrise.lblSunset.text="ayy lmao";
xmlhttp.suspend();
return;
}

任何帮助欢迎! (我尝试过将onReadyStateChange行放置在打开上方和下方,但无论SunsetCallback函数是否执行。)

问题是iOS不再允许应用程序无需编辑plist即可发送获取请求。 当我将请求更改为https并将App Transport Security设置字典添加到plist时,此问题已解决。

在对响应进行任何操作之前,您需要检查请求的状态。 状态为常数后即可执行操作。HTTP_READY_STATE_DONE。

请检查以下工作代码:

function searchData(){
  srchKey = frmMain.txtData.text;
  var request = new kony.net.HttpRequest();
  request.onReadyStateChange=httpCallbackHandler;
  request.open(constants.HTTP_METHOD_GET, "http://yoururlorapi.com/search/"+srchKey);
  request.setRequestHeader("x-header-appsecret",appsec); //Our own data
  request.send();
}

function httpCallbackHandler(){
  if(this.readyState == constants.HTTP_READY_STATE_DONE){     
    //alert(JSON.stringify(this.response));
    frmSearchRes.segRes.setData(this.response.searchInfo);
    frmSearchRes.show();
  }
}

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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