Click here to Skip to main content
15,917,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How Can I use fetch command in Visual Studio Code ?

it can't recognize fetch command

What I have tried:

JavaScript
var _currentTime;
var _timeDifference = 0;
var d1;
var _currentTime


const _runAtFullTime = new Date(2021, 5, 1, 6, 10, 59, 920);
const _runAtEpoch = _runAtFullTime.getTime();

console.log(_runAtEpoch);



while (true) {
    _d1 = new Date();
    _currentTime = _d1.getTime();
    _trigger = _runAtEpoch - _currentTime;
    console.log(_trigger);

    if (_trigger <= 0) {
SendRequest();        
console.log('Successfull');
        break;
    }

}


function SendRequest() {

    var myHeaders = new Headers();
    myHeaders.append("Connection", "keep-alive");
    myHeaders.append("sec-ch-ua", "\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"91\", \"Chromium\";v=\"91\"");
    myHeaders.append("sec-ch-ua-mobile", "?0");
    myHeaders.append("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36");
    myHeaders.append("Content-Type", "text/plain");
    myHeaders.append("Accept", "*/*");
    myHeaders.append("Origin", "Somethimg");
    myHeaders.append("Sec-Fetch-Site", "same-origin");
    myHeaders.append("Sec-Fetch-Mode", "cors");
    myHeaders.append("Sec-Fetch-Dest", "empty");
    myHeaders.append("Referer", "Something");
    myHeaders.append("Accept-Language", "en-US,en;q=0.9,fa;q=0.8,tr;q=0.7,de;q=0.6");
    myHeaders.append("Cookie", "cookiesession1=462A2B80VWZSTW26WF3QA0KE3VCYE820; DWRSESSIONID=YFe7h23EAe~OyKWGJDfDtCxusD5EkutPaDn; JSESSIONID=2B6C81A73B27F520D4D5BFC14BEAABA4");

    var raw = "callCount=1\n\nnextReverseAjaxIndex=0\n\nc0-scriptName=AjaxManager\n\nc0-methodName=saveMMTPOrder\n\nc0-id=0\n\nc0-param0=string:2B6C81A73B27F520D4D5BFC14BEAABA4\n\nc0-param1=string:\n\nc0-param2=string:IRO1GNBO0001\n\nc0-param3=string:15890062575775%20%20\n\nc0-param4=string:V\n\nc0-param5=string:2310\n\nc0-param6=string:0\n\nc0-param7=string:\n\nc0-param8=string:67140\n\nc0-param9=string:L\n\nc0-param10=string:J\n\nc0-param11=string:\n\nc0-param12=number:736467\n\nc0-param13=string:2\n\nc0-param14=number:1622694638706\n\nc0-param15=string:\n\nc0-param16=string:-1\n\nc0-param17=string:1\n\nc0-param18=null:null\n\nc0-param19=boolean:false\n\nc0-param20=string:1\n\nbatchId=10\n\ninstanceId=1\n\npage=%2FmmtpOrder.do%3F0.6389452411363696\n\nscriptSessionId=YFe7h23EAe~OyKWGJDfDtCxusD5EkutPaDn/xhH9gDn-Ar5jBbanv\n\n";

    var requestOptions = {
        method: 'POST',
        headers: myHeaders,
        body: raw,
        redirect: 'follow'
    };

    fetch("MyUrl", requestOptions)
        .then(response => response.text())
        .then(result => console.log(result))
        .catch(error => console.log('error', error));
    
}
Posted
Updated 2-Jun-21 22:31pm
v2

Assuming you're running code within VS Code itself (which will run on node.js) rather than in a browser, you'll need to install a node.js library to support fetch.

There are several suggestions on this StackOverflow thread:
javascript - ReferenceError: fetch is not defined - Stack Overflow[^]
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900