Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between fetch and jquery ajax?

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) {
}
like image 276
Zuckjet Avatar asked Mar 25 '17 14:03

Zuckjet


People also ask

What is the difference between Ajax and fetch?

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.

Does jQuery Ajax use 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.

Should I use fetch or jQuery?

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.

What is the difference between Ajax and jQuery?

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.


1 Answers

Fetch specification differs from jQuery.ajax() in mainly two ways:

  1. 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.

  2. 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).

like image 164
Rytis Dereskevicius Avatar answered Oct 13 '22 13:10

Rytis Dereskevicius