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

怎么解决跨域问题呢? #5

Open
yangyiru opened this issue Sep 28, 2017 · 1 comment
Open

怎么解决跨域问题呢? #5

yangyiru opened this issue Sep 28, 2017 · 1 comment

Comments

@yangyiru
Copy link

No description provided.

@Panda-Hope
Copy link
Owner

1:通过JSONP来解决跨域问题,不过这个需要对方服务器做相应的处理。
例如:
function jsonp(urlObj, callback) {
let url = urlObj.url,
callbackName = urlObj.jsonpCallback;

window[callbackName] = function(data) {
    window[callbackName] = undefined;
    document.body.removeChild(script);
    callback(data);
};

let script = document.createElement('script');
script.src = url;
document.body.appendChild(script);

}

2:常用强力解决办法,打开你的webpack devserver 通过这个进行代理配置
例如:
devServer: {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
"Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization"
},
proxy: {
'/OAAccount': {
target: REMOTE_URL,
secure: false,
changeOrigin: true
}
}
}
详情请参考webpack devserver 文档 https://webpack.js.org/configuration/dev-server/#devserver-proxy

3: 最简单的方法, 服务端将cross-origin:*这样设置一下即可,需服务端主动配置

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants