如何在基于IP的URL上的fetch(对于POST API)中设置cookie

时间:2016-07-24 21:04:49

标签: javascript rest cookies reactjs fetch

我在reactJS中使用fetch for REST API,并使用

传递cookie
 credentials: "include"

但它不适用于(**基于IP的网址)。这是我的响应标头配置。

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:X-Requested-With, X-CSRF-Token, X-Auth-Token, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding
Access-Control-Allow-Methods:POST, GET
Access-Control-Allow-Origin:http://localhost:8080
Content-Length:93
Content-Type:application/json; charset=UTF-8
Date:Sun, 24 Jul 2016 20:29:48 GMT

**当我正在做POST请求时,我的本地IP是192.168.1.4,cookie没有发送到服务器

 fetch('http://192.168.1.4:9000/test', {
        credentials: "include",
        method: 'post',
        mode: 'cors',   
        headers: {
            'Accept': 'application/json'
        },
        body: JSON.stringify({
           "username":"abd",
           "passwd":"Changemes1"
        }) 

这是本地ip的请求标头(192.168.1.4)

accept:application/json
Accept-Encoding:gzip, deflate
Accept-Language:en-GB,en;q=0.8,en-US;q=0.6,hi;q=0.4,ru;q=0.2
Cache-Control:no-cache
Connection:keep-alive
Content-Length:115
content-type:text/plain;charset=UTF-8
Host:192.168.1.4:9000
Origin:http://localhost:8080
Pragma:no-cache
Referer:http://localhost:8080/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36

但是当我使用localhost而不是我的本地ip(192.168.1.4)那么浏览器完美地发送COOKIE

 fetch('http://localhost:9000/test', {
            credentials: "include",
            method: 'post',
            mode: 'cors',   
            headers: {
                'Accept': 'application/json'
            },
            body: JSON.stringify({
               "username":"abd",
               "passwd":"Changemes1"
            })

请在此处(请求标题, Cookie:Token = John Doe 目前) -

accept:application/json
Accept-Encoding:gzip, deflate
Accept-Language:en-GB,en;q=0.8,en-US;q=0.6,hi;q=0.4,ru;q=0.2
Cache-Control:no-cache
Connection:keep-alive
Content-Length:115
content-type:text/plain;charset=UTF-8
Cookie:Token=John Doe
Host:localhost:9000
Origin:http://localhost:8080
Pragma:no-cache
Referer:http://localhost:8080/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36

基于IP的网址有什么问题请建议我。

0 个答案:

没有答案