Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch API cannot load error with cors in chrome/firefox #301

Closed
mkaushik10 opened this issue Mar 29, 2016 · 5 comments
Closed

fetch API cannot load error with cors in chrome/firefox #301

mkaushik10 opened this issue Mar 29, 2016 · 5 comments

Comments

@mkaushik10
Copy link

Hi,
I have a REST API which allows my UI url as origin. My web UI is using fetch to get and post data by choosing 'mode: cors'. This works fine in IE but not in chrome/firefox.
I am using below code to get data from my api which is on a different domain.

fetch(url, {
     mode: 'cors',
});

But I get this error.
image
Any help with this would be greatly appreciated. Any pointers on how to get this to work with chrome/firefox?
Thanks.

@dgraham
Copy link
Contributor

dgraham commented Mar 29, 2016

The browser's error message describes the requirements for this request to complete successfully. The server must grant access to the requested resource to your remote domain by sending a Access-Control-Allow-Origin response header.

These past tips might help too:

@dgraham dgraham closed this as completed Mar 29, 2016
@voidException
Copy link

let defaultOptions = {
url:'',
method:'POST',
mode: 'cors',
headers:{
'Access-Control-Allow-Origin':'*'
},
body:null,
};

let UploadFile=function(options){

let header = new Headers({
    'Access-Control-Allow-Origin':'*',
    'Content-Type': 'multipart/form-data'
});
let opt = Object.assign({}, defaultOptions, options); //将默认的参数和传过来的合并在一起
let sentData={
    method:opt.method,
    mode: 'cors',
    header: header,
    body:opt.body || ''
};
return new Promise((reslove,reject)=>{
    fetch(opt.url, sentData)
        .then(response=> response.json())
        .then(responseText=>{
            let resp = typeof responseText === 'string' ? JSON.parse(responseText) : responseText;
            //console.log(resp);
            reslove(resp); //这个resp会被外部接收
        }).catch(err=>{
        //console.log(err);
        reject(err);
    });
}).catch(err => {
        console.log('出错了');
    });

}
export default UploadFile;

you must use
let header = new Headers({
'Access-Control-Allow-Origin':'',
'Content-Type': 'multipart/form-data'
});
instead of let defaultOptions = {
url:'',
method:'POST',
mode: 'cors',
headers:{
'Access-Control-Allow-Origin':'
'
},
body:null,
};
to add 'Access-Control-Allow-Origin':'*'

@voidException
Copy link

when you user spirngmvc ,you should do as :
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public LoginDto logout(HttpServletRequest request, HttpServletResponse resp) {
resp.setHeader("Access-Control-Allow-Origin", "*");
LoginDto loginDto = new LoginDto();
request.getSession().removeAttribute("user");
loginDto.setMessage(ResultMessage.SUCCESS);
return loginDto;
}

then it works !

@futurechallenger
Copy link

Well, If you use loalhost or 127.0.0.1 as I do in Google Chrome you will definitely met this problem, you would like to use a chrome Extension to get fetch work. This is all in a stack overflow answer

Repository owner locked and limited conversation to collaborators Dec 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@dgraham @futurechallenger @mkaushik10 @voidException and others