I want to send a post request through fetch, but it does not work.
我想通过fetch发送一个帖子请求,但它不起作用。
But if I do it through jQuery ajax, it succeeds.
但如果我通过jQuery ajax做到这一点,它就会成功。
I want to know the difference of the two way and if there is anything wrong in my use of fetch here:
我想知道这两种方式的区别,如果我在这里使用fetch有什么问题:
fetch('http://localhost:8888/news',{
method:"post",
data:"code=7&a=8&b=9"
}).then(function(data){
data.json().then(function (json) {
}
1 个解决方案
#1
6
Fetch
specification differs from jQuery.ajax()
in mainly two ways:
获取规范与jQuery.ajax()的不同之处主要有两种:
-
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.
即使响应是HTTP 404或500,fetch()返回的Promise也不会拒绝HTTP错误状态。相反,它将正常解析(ok状态设置为false),它只会拒绝网络故障或如果有任何事情妨碍了请求的完成。
-
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).
默认情况下,fetch不会从服务器发送或接收任何cookie,如果站点依赖于维护用户会话,则会导致未经身份验证的请求(要发送cookie,必须设置凭证init选项)。
#1
6
Fetch
specification differs from jQuery.ajax()
in mainly two ways:
获取规范与jQuery.ajax()的不同之处主要有两种:
-
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.
即使响应是HTTP 404或500,fetch()返回的Promise也不会拒绝HTTP错误状态。相反,它将正常解析(ok状态设置为false),它只会拒绝网络故障或如果有任何事情妨碍了请求的完成。
-
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).
默认情况下,fetch不会从服务器发送或接收任何cookie,如果站点依赖于维护用户会话,则会导致未经身份验证的请求(要发送cookie,必须设置凭证init选项)。