如何使用JavaScript在AngularJS 2.0中调用Restful API
How to Call Restful API in AngularJS 2.0 using JavaScript?
我有一些表单的示例代码,现在我想将这些数据发布到Restful API。我检查了很多,但大部分都是基于TypeScript的结果。我想只使用JavaScript。有没有什么方法可以使用JavaScript来做到这一点?
我想在"onSubmit"事件上调用Postneneneba 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 = {}));
如中所示https://stackoverflow.com/a/45529553/512089
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 如何从Java/scala调用js美化程序
- 如何用JavaScriptEasy的方式一次调用大量restful服务
- 来自 AngularJS 和查询字符串的 Restful API 调用
- 为什么无法在 Jquery/ajax 调用中获取对 RESTful 服务的成功回调
- 从angularjs调用RESTful服务的正确方法
- 来自链接函数的Restful web service调用
- 如何使用JavaScript在AngularJS 2.0中调用Restful API
- 来自Javascript/Django的POST RESTful api调用
- 对restful服务进行非HTTP调用
- RESTful API调用没有更新本地数组
- 使用POST调用RESTFUL web服务
- 在javascript中调用restful webservice时如何传递对象
- 从javascript调用Java restful webservice时返回值失败
- 如何将RESTful POST调用转换为Tornado中的Ajax
- 在angularjs中调用http (RESTFUL WebAPI)的正确方法
- 我应该通过节点应用程序中的restful api进行所有数据库调用吗
- 所有RESTful API调用都应该在角度服务中定义吗
- web服务-使用javascript调用expressjs restful webservice =>结果总是空的响应