在服务器端,你不需要做任何事情——要插入的项目没有 id 字段就进入服务器,然后返回一个。例如,这是一个向表中插入数据的典型请求:
POST /tables/people HTTP/1.1
Content-Type: application/json
Content-Length: ...
Host: myservice.azure-mobile.net
{"name":"John Doe","age":33}
这将是它的回应:
HTTP/1.1 201 Created
Content-Type: application/json
Date: ...
Content-Length: ...
{"name":"John Doe","age":33,"id":234}
在客户端,您可以在请求完成后访问 id。如果您使用的是托管语言,则可以这样做:
var item = new Person { Name = "John Doe", Age = 33 };
await table.InsertAsync(item);
// When the call returns, the Id field is populated with the id from the server
var newId = item.Id;
或者在 JavaScript 中:
var item = { name: 'John Doe', age: 33 };
table.insert(item).done(function(inserted) {
var id = inserted.id;
});
或者在 Objective-C 中:
NSDictionary *item = @{@"name":@"John Doe",@"age":@33};
[table insert:item completion:^(NSDictionary *inserted, NSError *insertError) {
NSNumber *id = [inserted objectForKey:@"id"];
}];