无法 post JSON 到 titanium appcelerator 中的服务器
Can not post JSON to server in titanium appcelerator
以下是我的规格我是 运行:
Operating System Name = Mac OS X Version
= 10.9.5 Architecture = 64bit
Titanium CLI CLI Version = 3.4.1 Titanium SDK SDK
Version = 3.5.0.GA Target Platform =
android
Ex:-> Node.js Node.js Version = 0.10.24 npm Version
= 1.3.21
这是我的代码:
var url = "http://192.168.1.100/test.php";
var xhr = Titanium.Network.createHTTPClient();
xhr.onerror = function(e) {
swim.lib.err('' + e.error);
alert('HTTP ERR...');
};
xhr.onload = function(e) {
Ti.API.info(this.responseText);
alert('success');
};
xhr.open("POST", url);
//post/get and your URL
xhr.setRequestHeader("content-type", "application/json");
var param = {
"user" : {
"email" : "someone@email.com",
"password" : "secure"
}
};
xhr.send(JSON.stringify(param));
您的服务器期望什么?我刚刚测试了您的代码,将 url 替换为 http://requestb.in/157oi2x1 and that seems fine, as you can see on http://requestb.in/157oi2x1?inspect:
Headers
Via: 1.1 vegur
Connect-Time: 1
User-Agent: Appcelerator Titanium/3.5.0 (iPhone Simulator/8.2; iPhone OS; en_US;)
Host: requestb.in
Total-Route-Time: 0
X-Requested-With: XMLHttpRequest
Content-Type: application/json
Accept-Language: en-us
X-Titanium-Id: d6caf6df-6a27-49b0-ad38-1c0d64356393
X-Request-Id: 257ddc46-066d-4c1d-95d6-cf6a642dbeab
Content-Length: 58
Accept: */*
Connection: close
Accept-Encoding: gzip, deflate
Body
{"user":{"email":"someone@email.com","password":"secure"}}
以下是我的规格我是 运行:
Operating System Name = Mac OS X Version
= 10.9.5 Architecture = 64bit
Titanium CLI CLI Version = 3.4.1 Titanium SDK SDK Version = 3.5.0.GA Target Platform = androidEx:-> Node.js Node.js Version = 0.10.24 npm Version
= 1.3.21
这是我的代码:
var url = "http://192.168.1.100/test.php";
var xhr = Titanium.Network.createHTTPClient();
xhr.onerror = function(e) {
swim.lib.err('' + e.error);
alert('HTTP ERR...');
};
xhr.onload = function(e) {
Ti.API.info(this.responseText);
alert('success');
};
xhr.open("POST", url);
//post/get and your URL
xhr.setRequestHeader("content-type", "application/json");
var param = {
"user" : {
"email" : "someone@email.com",
"password" : "secure"
}
};
xhr.send(JSON.stringify(param));
您的服务器期望什么?我刚刚测试了您的代码,将 url 替换为 http://requestb.in/157oi2x1 and that seems fine, as you can see on http://requestb.in/157oi2x1?inspect:
Headers
Via: 1.1 vegur
Connect-Time: 1
User-Agent: Appcelerator Titanium/3.5.0 (iPhone Simulator/8.2; iPhone OS; en_US;)
Host: requestb.in
Total-Route-Time: 0
X-Requested-With: XMLHttpRequest
Content-Type: application/json
Accept-Language: en-us
X-Titanium-Id: d6caf6df-6a27-49b0-ad38-1c0d64356393
X-Request-Id: 257ddc46-066d-4c1d-95d6-cf6a642dbeab
Content-Length: 58
Accept: */*
Connection: close
Accept-Encoding: gzip, deflate
Body
{"user":{"email":"someone@email.com","password":"secure"}}