添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
慷慨大方的皮带  ·  vue3+ts:shims-vue.d.ts·  1 月前    · 
叛逆的山楂  ·  调试 JavaScript 或 ...·  1 月前    · 
淡定的登山鞋  ·  python folium 库学习 - ...·  1 年前    · 
八块腹肌的大熊猫  ·  必须服气! ...·  1 年前    · 
捣蛋的皮带  ·  Statistics In ...·  2 年前    · 
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?

That's not TypeScript. Please add a minimal, complete, and verifiable example which shows the actual problem. – Andreas Dec 15, 2017 at 11:43 Can you try with await fetch("/LostEnergyCalculation/GetGridAndPieChartsData"+"/"+param1+"/"+param2) you have to handle the route in MVC – Niladri Dec 15, 2017 at 12:03

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);
                Unfortunately, I'm not able to make it work in way you presented. It works in another way I've found in the meantime. It's in separated comment bellow.
– tesicg
                Dec 27, 2017 at 6:17
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.