I am trying to find an example of HTTP post request in Typescript but can only find examples that use Angular. Could someone point me in the right direction to find this or post a quick example here of using Post with JSON data to get a response JSON.
相关问题
- Angular RxJS mergeMap types
- void before promise syntax
- Ignore Typescript errors in Webpack-dev-server
- HTML form is not sending $_POST values
- Google Apps Script: testing doPost() with cURL
Update 2020:
Note that as of now, the global
fetch
is available on all modern browsers and covers 95% of all web users. If you need support for IE10 or before, read the original answer.MDN Doc | TypeScript Definition
Where the function is available in the
window
orglobal
contexts, looks like:so you can do:
Original Answer:
If you want to use native JavaScript functions in TypeScript for your HTTP POST request, take a look at the JSON and POST examples on YouMightNotNeedJQuery.com. Using that, you can implement your own:
XMLHttpRequest
is a built-in JavaScript class and included in the TypeScript typings.Here is my very simple example to call GET or POST with Typescript only.
Optional input data (the post body) and callback. The data and result are both assumed to be JSON.
Sending form data.