Ajax 允许交叉来源-铁-聚合物
我准备好了Ajax 允许交叉来源-铁-聚合物,ajax,polymer,cors,Ajax,Polymer,Cors,我准备好了 XMLHttpRequest cannot load http://abcd.com/xyz?id=1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. 好了,问题终于解决了。我会解释到底需要做什么 首先,如上面的评论所述,需要对要向其发送请求的服务
XMLHttpRequest cannot load http://abcd.com/xyz?id=1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
好了,问题终于解决了。我会解释到底需要做什么 首先,如上面的评论所述,需要对要向其发送请求的服务器进行更改。在我的例子中,它是一个ApacheTomcat服务器。因此,我对
web.xml
文件做了以下更改,现在一切正常。
请注意,在下面的代码中,允许访问任何其他域。但是,将其仅限于您自己的域(受信任的域)是一种安全的做法。因此,您可以指定自己的域,而不是*
/**
* @license
* Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
"use strict";
const express = require('express');
const http = require('http');
const path = require('path');
const send = require('send');
const url = require('url');
const make_app_1 = require('./make_app');
const findPort = require('find-port');
const opn = require('opn');
function applyDefaultOptions(options) {
const withDefaults = Object.assign({}, options);
Object.assign(withDefaults, {
port: options.port || 8080,
hostname: options.hostname || "localhost",
root: path.resolve(options.root || '.'),
});
return withDefaults;
}
/**
* @return {Promise} A Promise that completes when the server has started.
*/
function startServer(options) {
return new Promise((resolve, reject) => {
options = options || {};
if (options.port) {
resolve(options);
}
else {
findPort(8080, 8180, (ports) => {
options.port = ports[0];
resolve(options);
});
}
}).then((opts) => startWithPort(opts));
}
exports.startServer = startServer;
const portInUseMessage = (port) => `
ERROR: Port in use: ${port}
Please choose another port, or let an unused port be chosen automatically.
`;
function getApp(options) {
const port = options.port;
const hostname = options.hostname;
const root = options.root;
const app = express();
console.log(`Starting Polyserve...
serving on port: ${port}
from root: ${root}
`);
const polyserve = make_app_1.makeApp({
componentDir: options.componentDir,
packageName: options.packageName,
root,
});
options.packageName = polyserve.packageName;
const filePathRegex = /.*\/.+\..{1,}$/;
app.use('/components/', polyserve);
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/*', (req, res) => {
const filePath = req.path;
send(req, filePath, { root: root, })
.on('error', (error) => {
if ((error).status == 404 && !filePathRegex.test(filePath)) {
send(req, '/', { root: root }).pipe(res);
}
else {
res.statusCode = error.status || 500;
res.end(error.message);
}
})
.pipe(res);
});
return app;
}
exports.getApp = getApp;
/**
* Open the given web page URL. If no browser keyword is provided, `opn` will use
* the user's default browser.
*/
function openWebPage(url, withBrowser) {
const openOptions = {
app: withBrowser
};
opn(url, openOptions, (err) => {
if (err) {
// log error and continue
console.error(`ERROR: Problem launching "${openOptions.app || 'default web browser'}".`);
}
});
}
function startWithPort(userOptions) {
const options = applyDefaultOptions(userOptions);
const app = getApp(options);
let server = http.createServer(app);
let serverStartedResolve;
let serverStartedReject;
const serverStartedPromise = new Promise((resolve, reject) => {
serverStartedResolve = resolve;
serverStartedReject = reject;
});
server = app.listen(options.port, options.hostname, () => serverStartedResolve(server));
server.on('error', function (err) {
if (err.code === 'EADDRINUSE') {
console.error(portInUseMessage(options.port));
}
serverStartedReject(err);
});
const serverUrl = {
protocol: 'http',
hostname: options.hostname,
port: `${options.port}`,
};
const componentUrl = Object.assign({}, serverUrl);
componentUrl.pathname = `components/${options.packageName}/`;
console.log(`Files in this directory are available under the following URLs
applications: ${url.format(serverUrl)}
reusable components: ${url.format(componentUrl)}`);
if (options.open) {
let openUrl;
if (options.openPath) {
openUrl = Object.assign({}, serverUrl);
openUrl.pathname = options.openPath;
}
else {
openUrl = Object.assign({}, componentUrl);
}
if (!Array.isArray(options.browser)) {
openWebPage(url.format(openUrl));
}
else {
options.browser.forEach((browser) => {
openWebPage(url.format(openUrl), browser);
});
}
}
return serverStartedPromise;
}
克斯菲尔特
org.apache.catalina.filters.CorsFilter
科尔斯
*
cors.methods
获取、发布、头部、选项、放置
cors.allowed.headers
内容类型、X-Requested-With、accept、Origin、访问控制请求方法、访问控制请求标头
cors.exposed.headers
访问控制允许来源,访问控制允许凭据
cors.support.credentials
真的
cors.preflight.maxage
10
克斯菲尔特
/*
如果使用spring boot,还可以为WebMVCConfigureAdapter定义全局配置:
<!-- Enabled CORS (Start) -->
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Enabled CORS (END) -->
您是否拥有更改代码的双方权限?客户端和服务器?@Altmish-E-Azam是的。我正在使用node js server。添加一个标题“Access Control Allow Origin”,并在
http://abcd.com/xyz
@Altmish-E-Azam在客户端?例如,`头文件=“{”访问控制允许源文件“}”`。或者是在某个节点js配置文件中完成。我不太确定我需要在哪个文件中进行这些更改。老实说,我对node js不太了解,但是如果你可以添加上面的头和值,那么它将不需要任何扩展名。
<!-- Enabled CORS (Start) -->
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Enabled CORS (END) -->
`@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://127.0.0.1:8081");
}
};
}