当用户提交 POST 请求时,我需要生成 GUID。我以前从未使用过 GUID,所以如果有人能在这里帮助我,那就太好了。这些是我现在拥有的代码
对于 GUID
constructor(public http: HttpClient ) {
class Guid {
static newGuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
}
// Example of a bunch of GUIDs
for (var i = 0; i < 100; i++) {
var id = Guid.newGuid();
console.log(id);
}}
我需要为其生成 GUID 的 API:
updateSpin(spin_result_id: AAGUID, inventory_id: AAGUID, status: string){
return Observable.create(observer => {
this.http.post(`${this.dataApiUrl}//some api`,
{records: [{
"inventory_id": inventory_id,
"status": status
}],
resource_id: '//some api'},
{headers: new HttpHeaders().set('X-XSRF-TOKEN', this.getCookie('XSRF-TOKEN')),
responseType: 'text'})
.subscribe(data => {
console.log(data);
observer.next(true);
observer.complete(true);
}, error => {
console.log(error);
observer.next(false);
observer.complete();
})
});}
我尝试过在线搜索,但我不太明白如何在当前代码中实现 GUID...
最佳答案
测试此功能:
public static generateGUID() {
function S4() {
return (((1 + Math.random()) * 0x10000) || 0).toString(16).substring(1);
}
return (S4() + S4());
}
console.log(generateGUID());
希望我的帮助有效ツ
关于javascript - 使用 POST 请求生成 GUID - Ionic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53740349/