使用Titanium的RESTful服务

Consuming RESTful services from Titanium

本文关键字:服务 RESTful Titanium 使用      更新时间:2023-09-26

是否有从Titanium执行HTTP PUT/DELETE方法的方法?他们得到支持了吗?是否有任何第三方库或解决方法来处理它?

是的,您可以在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();

请注意,如果您尝试向主体添加参数,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