Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I've used the following code:
let response = await fetch("/LostEnergyCalculation/GetGridAndPieChartsData");
let data = await response.json();
without parameters and it works fine, but how to use it with parameters?
–
–
If you need to POST
data in your fetch, you can pass a body
with the request...
The example below passes form style data in the request, and returns typed data using an interface.
interface MyApiData {
name: string;
class Example {
async getData(foo: string, bar: string) {
let response = await fetch(
'/LostEnergyCalculation/GetGridAndPieChartsData',
method: 'post',
headers:new Headers({
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
body: `foo=${foo}&bar=${bar}`
let data: MyApiData = await response.json();
return data;
const example = new Example();
example.getData('foovalue', 'barvalue').then((data) => {
alert(data.name);
–
async onFieldSelectionChanged() {
const selectedOilField = $("#OilFieldsList").dxSelectBox("instance");
this.oilFieldId = selectedOilField.option("value");
if (this.oilFieldId === null) {
selectedOilField.option("value", 1);
this.oilFieldId = 1;
if (this.initialLoad) {
const startDateBox = $("#StartDate").dxDateBox("instance");
this.startDate = startDateBox.option("value").toISOString();
const endDateBox = $("#EndDate").dxDateBox("instance");
this.endDate = endDateBox.option("value").toISOString();
const periodGroup = $("#DateTimePeriod").dxRadioGroup("instance");
this.period = periodGroup.option("value");
//$.ajax({
// type: "POST",
// url: "/LossEnergyCalculation/GetGridAndPieChartsData",
// data: { oilFieldId: this.oilFieldId, periodGroup: this.period, startDate: this.startDate, endDate: this.endDate }
//}).done(data => {
// this.showDataGrid(data);
// this.showRealLossDonutChart(data);
// this.showNormativeLossDonutChart(data);
// this.initialLoad = false;
//}).fail(function (jqXHR, textStatus, errorThrown) {
// console.log("jqXHR: " + jqXHR.responseText);
// console.log("jqXHR status: " + jqXHR.status);
// console.log("textStatus: " + textStatus);
// console.log("errorThrown: " + errorThrown);
//});
const response = await fetch("/LossEnergyCalculation/GetGridAndPieChartsData",
method: "post",
headers: new Headers({
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
body: "oilFieldId=${this.oilFieldId}&periodGroup=${this.period}&startDate=${this.startDate}&endDate=${this.endDate}"
const data = await response.json();
} else {
return;
It fails on calling fetch method with an error that you can take a look at the screen shot attached.
By the way, the commented ajax call works fine.
`oilFieldId=${this.oilFieldId}`,
`periodGroup=${this.period}`,
`startDate=${this.startDate}`,
`endDate=${this.endDate}`
].join('&');
const request = new Request(`/LossEnergyCalculation/GetGridAndPieChartsData?${queryString}`,
method: "POST"
const data = await fetch(request).then(response => response.json());
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.