从Titanium使用RESTful服务

时间:2012-02-19 08:14:09

标签: javascript http titanium

无论如何都要从Titanium执行HTTP PUT / DELETE方法吗?他们支持吗?是否有任何第三方图书馆或解决方法处理它?<​​/ p>

2 个答案:

答案 0 :(得分:2)

是的,您可以在HTTPClient

中将PUT / DELETE作为动词发送
var url = "http://www.appcelerator.com";
 var client = Ti.Network.createHTTPClient({
     // function called when the response data is available
     onload : function(e) {
         Ti.API.info("Received text: " + this.responseText);
         alert('success');
     },
     // function called when an error occurs, including a timeout
     onerror : function(e) {
         Ti.API.debug(e.error);
         alert('error');
     },
     timeout : 5000  /* in milliseconds */
 });


 // Prepare the connection.
 // This accepts PUT/DELETE/GET/POST
 client.open("PUT", url);
 // Send the request.
 client.send();

答案 1 :(得分:0)

请注意,如果您尝试向主体添加参数,Titanium(HttpClient)会自动将DELETE转换为POST,并且不会给出任何明确的指示。必须在查询字符串中传递DELETE上的参数。

执行此转换的代码位于ASIHTTPRequest.m

if ([self postLength] > 0) {
    if ([requestMethod isEqualToString:@"GET"] || [requestMethod isEqualToString:@"DELETE"] || [requestMethod isEqualToString:@"HEAD"]) {
        [self setRequestMethod:@"POST"];
    }
    [self addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%llu",[self postLength]]];
}
[self setHaveBuiltPostBody:YES];

来源:http://developer.appcelerator.com/question/123042/only-get-and-post-methods-in-httpclient--no-put-or-delete