GVKun编程网logo

Ajax - CORS - Cross Origin Resource Sharing

14

在本文中,我们将为您详细介绍Ajax-CORS-CrossOriginResourceSharing的相关知识,此外,我们还会提供一些关于AResourcePoolcouldnotacquireare

在本文中,我们将为您详细介绍Ajax - CORS - Cross Origin Resource Sharing的相关知识,此外,我们还会提供一些关于A ResourcePool could not acquire a resource from its primary factory or source.、ajax – 由watchguard剥离的CORS Access-Control-Allow-Origin标头、ajax 请求 Cross origin requests are only ...、Ajax-'Origin localhost不允许Access-Control-Allow-Origin'的有用信息。

本文目录一览:

Ajax - CORS - Cross Origin Resource Sharing

Ajax - CORS - Cross Origin Resource Sharing

Below Issue only happens while trying to access some resources which are existing in a different origin

  • Issue Description
    Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8089/webapp/jsp/login. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

  • Operations caused the issue
    Using ajax to access some other web source,like below:

<a onclick="valueAjax()">item2</a>
function valueAjax(){
  $.get("http://www.baidu.com",function (data){
             document.getElementById('a').innerHTML = data;
  }).fail(function(data) {
    alert( data);
  });
}
  • Reason analysis
    The resource which is accessing does not exist in the same origin with the requester.

  • How to resolve

    1. Simulation Environment:
      Web Server: tomcat
      Web page: jsp,html
      Script: Jquery
      a. Create html ‘page1.html’
<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="jquery-1.11.3.js"></script>
</head>

<body>
<div style="position: absolute; top:10px; left: 10px; border: solid 1px red; width: 100px; height: 100px" id="left">
   <button onclick="changeValue(this.innerHTML)">item1</button>
   <a onclick="valueAjax()">item2</a>
</div>
<div style="position: absolute; top:10px; left: 120px; border: solid 1px red; width: 100px; height: 100px" id="right">
   <span id="a">asf</span>
   <span>item2</span>
</div>

<script> function changeValue(val){ alert(val); document.getElementById('a').innerHTML = val; } function valueAjax(){ $.get("http://localhost:8089/webapp/jsp/login",function (data){ document.getElementById('a').innerHTML = data; }).fail(function(data) { alert( data); }); } </script>

</body>
</html>
  • Create Web project ‘webapp’,configure below filter in web.xml
<filter>
        <filter-name>CorsFilter</filter-name>
        <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CorsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  • Result
    Initial page -

    Succeed from ajax -

Additional kNowledge
- Same-origin Policy
An origin is defined as a combination of URI scheme,hostname,and port number. If all the three parts are the same,we can say the two resources are in the same origin.
Why use it is for security concern.

Quotation
1. http://enable-cors.org/server_tomcat.html
2. http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter
3. https://en.wikipedia.org/wiki/Same-origin_policy

A ResourcePool could not acquire a resource from its primary factory or source.

A ResourcePool could not acquire a resource from its primary factory or source.

严重: Servlet.service () for servlet [dispatcherServlet] in context with path [/heima_ssm_web1] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!
### The error may exist in com/itheima/ssm/dao/IProductDao.java (best guess)
### The error may involve com.itheima.ssm.dao.IProductDao.findAll
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!] with root cause
com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.

 

问题解决

1. 检查数据库配置;

2. 如果你和我一样也是虚拟机中的数据库,检查虚拟机中数据库是否还能链接

ajax – 由watchguard剥离的CORS Access-Control-Allow-Origin标头

ajax – 由watchguard剥离的CORS Access-Control-Allow-Origin标头

我在www.example.com上有一个网站,它使用 AJAX通过api.example.com上的nodejs-API访问 JSON. (跨域!)我需要PUT和DELETE作为HTTP方法,这就是为什么我不能使用jsonp,我查找了CORS如何工作,并通过Access-Control-Allow-Origin头实现了它.

当我在我的本地环境(www.example.dev和api.example.dev)上工作时,我的设置有效.我的API返回正确的标头(Access-Control-Allow-Origin),我成功地做了跨域请求.

当我在线部署时,我的设置不再起作用,因为标题被公司监视防火墙阻止.我可以要求允许我的公共API,但我担心其他人在他或她受限制的环境中会遇到同样的问题.

有没有其他方法可以解决这个问题?

解决方法

对于那些感兴趣的人:我使用easyXDM进行跨域消息传递.这解决了我的问题. http://easyxdm.net/wp/

ajax 请求 Cross origin requests are only ...

ajax 请求 Cross origin requests are only ...

ajax 请求 Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-ext

请求地址前加上 "http://"

Failed to load localhost:8081: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

 

Ajax-'Origin localhost不允许Access-Control-Allow-Origin'

Ajax-'Origin localhost不允许Access-Control-Allow-Origin'

如何解决Ajax-''Origin localhost不允许Access-Control-Allow-Origin''?

此错误是由于跨域资源共享中强加了限制。这已作为安全功能的一部分实现,以通过跨域调用来限制资源的客户端(域)。当您将请求发送到Web服务或api或类似工具时,它会在服务器或目标(此处是api)的请求中添加Origin标头,以验证请求是否来自授权来源。理想情况下,API /服务器应OriginRequest header收到的信息中查找,并可能针对允许向其提供资源的一组来源(域)进行验证。如果来自允许的域,它将在响应标头中添加与"Access- Control-Allow- Origin"值。也可以使用通配符,但是问题是,在获得通配符许可的情况下,任何人都可以发出请求并将其送达(有一些限制,例如通过Windows auth或cookie对api进行身份验证,不允许您发送该withCredentials*) )。使用通配符来源的响应标头不是一个好习惯,因为它对所有人开放。

这些是使用值设置响应头的方法:-

Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: http://yourdomain.com

您甚至可以在同一响应中添加多个Access-Control-Allow-Origin标头(我相信在大多数浏览器中都可以使用)

Access-Control-Allow-Origin: http://yourdomain1.com
Access-Control-Allow-Origin: http://yourdomain2.com
Access-Control-Allow-Origin: http://yourdomain3.com

在服务器端(c#语法),您可以这样做:

var sourceDomain = Request.Headers["Origin"]; //This gives the origin domain for the request
     Response.AppendHeader("Access-Control-Allow-Origin", sourceDomain ); //Set the response header with the origin value after validation (if any) .Depending on the type of application you are using Syntax may vary.

希望这可以帮助!!!

解决方法

我是Ajax的新手,只是受过此跨域调用的任务。我们的网页上有一个文本框,用户可用来执行公司名称搜索。通过单击文本框旁边的按钮,将请求Ajax调用。不幸的是,Web服务位于单独的域中,因此自然会引起问题。

以下是我使这项工作的最佳尝试。我还要注意,此调用的目的是以XML格式返回结果,该结果将success在请求的一部分中进行解析。

这又是错误消息:

Origin http://localhost:55152 is not allowed by Access-Control-Allow-Origin.

对于解决方法,我不知所措,将不胜感激任何想法。

function GetProgramDetails() {
    var URL = "http://quahildy01/xRMDRMA02/xrmservices/2011/OrganizationData.svc/AccountSet?$select=AccountId,Name,neu_UniqueId&$filter=startswith(Name,\''" + $(''.searchbox'').val() + "\'')";
    var request = $.ajax({
        type: ''POST'',url: URL,contentType: "application/x-www-form-urlencoded",crossDomain: true,dataType: XMLHttpRequest,success: function (data) {
            console.log(data);
            alert(data);
        },error: function (data) {
            console.log(data);
            alert("Unable to process your resquest at this time.");
        }
    });
}

我们今天的关于Ajax - CORS - Cross Origin Resource Sharing的分享已经告一段落,感谢您的关注,如果您想了解更多关于A ResourcePool could not acquire a resource from its primary factory or source.、ajax – 由watchguard剥离的CORS Access-Control-Allow-Origin标头、ajax 请求 Cross origin requests are only ...、Ajax-'Origin localhost不允许Access-Control-Allow-Origin'的相关信息,请在本站查询。

本文标签: