Fetch(' https://mywebsite.com/endpoint/',{method:' POST ', headers:{' Accept ':' Application/json ' ' Content-type ' : Span class= "token string" > ' Application/json ' }< Span class= "token punctuation", Body.stringify ({firstparam< Span class= "token punctuation" >: ' Yourvalue ' Secondparam ' Yourothervalue ' }})
Fetch ("/data.json"). Then (function (res) {
if (Res.ok) {
Res.json (). Then (function (data) {
Console.log (data.entries);
});
} else {
Console.log ("Looks like the response wasn ' t perfect, got status", Res.status);
}
}, Function (e) {
Console.log ("Fetch failed!", e);
});
' Accept ': ' Application/json ',
' Content-type ': ' Application/json
Fetch ("http://www.example.org/submit.php", {
Method: "POST",
Headers: {
"Content-type": "Application/x-www-form-urlencoded;charset=utf-8"
},
Body: "Firstname=nikhil&favcolor=blue&password=easytoguess"
}). Then (function (res) {
if (Res.ok) {
Alert ("perfect! Your settings is saved. ");
} else if (Res.status = = 401) {
Alert ("oops! You is not authorized. ");
}
}, Function (e) {
Alert ("Error submitting form!");
});
Fetch VS AJAX