I want to send a post request through fetch, but it does not work.
But if I do it through jQuery ajax, it succeeds.
I want to know the difference of the two way and if there is anything wrong in my use of fetch here:
fetch('http://localhost:8888/news',{
method:"post",
data:"code=7&a=8&b=9"
}).then(function(data){
data.json().then(function (json) {
}
Fetch is compatible with all recent browsers including Edge, but not with Internet Explorer. Therefore, if you are looking for maximum compatibility, you will continue to use Ajax to update a web page. If you also want to interact with the server, the WebSocket object is also more appropriate than fetch.
We can fetch data through the old school AJAX way: In the code given below, we are implementing an example of Jquery using the $get AJAX method, the code fetches data from the API endpoint and prints the JSON data on the console.
Difference between fetch() Vs jQuery.Fetch is similar to XMLHttpRequest , but the new API provides a more powerful and flexible feature set. The fetch specification differs from jQuery. ajax() in these ways: The Promise returned from fetch() won't reject HTTP error status even if the response is an HTTP 404 or 500.
AJAX is a web development technique for making asynchronous calls to the server. jQuery is a JavaScript library for designing and make some web development tasks easy. It makes it possible to run javascript outside of the browser. It works on the browser or outside the browser also.
Fetch
specification differs from jQuery.ajax()
in mainly two ways:
The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500. Instead, it will resolve normally (with ok status set to false), and it will only reject on network failure or if anything prevented the request from completing.
By default, fetch won't send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user session (to send cookies, the credentials init option must be set).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With