What is the difference between fetch and jquery ajax?

喜欢而已 提交于 2019-11-29 10:52:59

问题


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) {
}

回答1:


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



来源:https://stackoverflow.com/questions/43017576/what-is-the-difference-between-fetch-and-jquery-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!