【问题标题】:Sending a post httpclient request in Titanium在 Titanium 中发送 post httpclient 请求
【发布时间】:2015-03-04 00:52:37
【问题描述】:
var xhr=Titanium.Network.createHTTPClient({
    onload:function(e){
    if(this.status == '200'){
          Ti.API.info('got my response, http status code ' + this.status);
           if(this.readyState == 4){
                var response=JSON.parse(this.responseText);
                success = true;
                }
            else{
                alert('HTTP Ready State != 4');
            }           
        }
        else{
            alert('HTTP Error Response Status Code = '+this.status);
            Ti.API.error("Error =>"+this.response);
        }              
    },

});
xhr.onerror = function(e){ 
};

xhr.open("POST","http://localhost:23003/api/user?username=dp&password=123456" ,true);//ADD your URL

xhr.setRequestHeader("content-type", "application/json");

xhr.send();  // Taa da

您好,上面提到的是我的代码,我正在尝试向本地服务器进程发送一个发布请求,如 open 方法中所述。但是我使用了一些控制台消息来查看这些返回的对象

  1. this.responseText -- 此文本为空白。 (我知道 java 脚本返回 null 的方式不同,并且警报消息将“null”显示为 null。)
  2. this.status -- 返回“0”
  3. this.readyState 返回 4
  4. var 响应 = JSON.parse();是空白文本
  5. 我也尝试将内容类型更改为 -application/x-www-form-urlencoded,但结果相同

我不知道我错在哪里。任何帮助将不胜感激。感谢您的宝贵时间。

【问题讨论】:

  • a/ 您可以尝试将 url 中的 localhost 更改为您的 ip 地址。 b/ 尝试使用postman 检查网络服务以确认网络服务工作正常。

标签: javascript post titanium appcelerator onload


【解决方案1】:

您的服务器端代码似乎有问题,原因如下:

  var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {
      console.info(this.status);
      console.info(this.readyState);
      console.info(this.responseText);
    }
  });
  xhr.open("POST", "http://requestb.in/ynwa0gyn");
  xhr.setRequestHeader("content-type", "application/json");
  xhr.send();

正确给出:

[INFO]  200
[INFO]  4
[INFO]  ok

请访问http://requestb.in/ynwa0gyn?inspect 了解客户提出的请求。

【讨论】:

  • 您认为这可能与我的本地防火墙或其他原因有关吗?
  • 可以的。您必须找到一种方法来查看请求是如何进入您的服务器的,并查看这是否正确。
  • 您认为是浏览器安全设置还是 Windows 安全设置在这样做?
  • 这是一个 CORS 问题。我经历了这个html5rocks.com/en/tutorials/cors。我必须设置服务器来处理 CORS 请求,并且客户端应该包含链接中提到的一些额外的标头。或者可以使用 android 或 Iphone 模拟器/模拟器来运行它。这是一个纯粹的浏览器安全障碍。
猜你喜欢
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-29
相关资源
最近更新 更多