[英]How to Call Restful API in AngularJS 2.0 using JavaScript?
我有一些表单的示例代码,现在我想将该数据发布到Restful API。 我已经检查了很多,但主要是基于TypeScript找到了结果。 我想只使用JavaScript来做到这一点。 有没有办法使用JavaScript?
我想在“onSubmit”事件上调用Post API。 看看示例代码。
(function(app) {
app.FormComponent = ng.core
.Component({
selector: 'form',
templateUrl: 'app/form.component.html'
})
.Class({
constructor: function() {
},
onSubmit: function() {
console.log(this.model);
this.submitted = true;
},
});
})(window.app || (window.app = {}));
您可以使用fetch
来进行服务器调用:
(function(app) {
app.FormComponent = ng.core
.Component({
selector: 'form',
templateUrl: 'app/form.component.html'
})
.Class({
constructor: function() {},
onSubmit: function() {
console.log(this.model);
this.submitted = true;
const url = 'http://example.com';
fetch(url, {
method: 'POST',
body: this.model
//new FormData(document.getElementById('inputform'))
// -- or --
// body : JSON.stringify({
// user : document.getElementById('user').value,
// ...
// })
})
.then(
response => response.text() // .json(), etc.
// same as function(response) {return response.text();}
)
.then(html => console.log(html));
}
});
})(window.app || (window.app = {}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.