Javascript No'访问控制允许来源'在加载不同url时,请求的资源上存在标头
Javascript No 'Access-Control-Allow-Origin' header is present on the requested resource on load of different url
我正试图将不同url中的值加载到div中,加载后会出现错误。我试着用谷歌搜索了很多。我没有找到解决办法。我知道这个问题已经被问了,但仍然没有解决我的问题。
XMLHttpRequest cannot load http://bulksms.icubetech.com/api/checkbalance.php?user=&pass=. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.w3schools.com' is therefore not allowed access.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("http://bulksms.icubetech.com/api/checkbalance.php?user=&pass=");
});
});
</script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>
</body>
</html>
从http://bulksms.icubetech.com/api/checkbalance.php
得到的响应应该在其标头中包含Access-Control-Allow-Origin
。
应该是这样的:
Access-Control-Allow-Origin: yoursite.com
如果不能满足上述要求,就不能通过ajax访问此网站。
或者,您可以使用代理php脚本来完成这项工作。这个想法是在你自己的域中获得一个php脚本,与你试图访问的网站对话,并给你结果。这不会引起任何跨域问题,因为您的浏览器只与自己域中的php脚本通信。下面是一个代理脚本示例。请根据您的需要进行更改。
<?php
// Allowed hostname
define ('HOSTNAME', 'http://Your_Server/');
//returns the headers as an array
function getHeaders()
{
$headers = array();
foreach ($_SERVER as $k => $v)
{
if (substr($k, 0, 5) == "HTTP_")
{
$k = str_replace('_', ' ', substr($k, 5));
$k = str_replace(' ', '-', ucwords(strtolower($k)));
$headers[$k] = $v;
}
}
return $headers;
}
// Get the REST call path from the AJAX application
$path = $_REQUEST['path'];
$url = HOSTNAME.$path;
// Open the Curl session
$session = curl_init($url);
// If it's a POST, put the POST data in the body
if ($_POST['path']) {
$postvars = '';
while ($element = current($_POST)) {
$postvars .= key($_POST).'='.$element.'&';
next($_POST);
}
$postvars = substr($postvars, 0, -1); // removing trailing &
$postvars = str_replace('xml_version', 'xml version', $postvars); // fix xml declaration bug?
$postvars = stripslashes($postvars);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
}
else {
//If it's a post, but not a form post (like a SOAP request)
if ($_SERVER['REQUEST_METHOD']==='POST') {
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $HTTP_RAW_POST_DATA);
$headers = getHeaders();
$header_array = Array( "Content-Type: text/xml", "SOAPAction: " . $headers['SOAPAction']);
curl_setopt ($session, CURLOPT_HTTPHEADER, $header_array);
curl_setopt ($session, CURLOPT_CUSTOMREQUEST, "POST");
}
}
// Don't return HTTP headers. Do return the contents of the call
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Make the call
$xml = curl_exec($session);
// The web service returns XML. Set the Content-Type appropriately
header("Content-Type: text/xml");
echo $xml;
curl_close($session);
?>
此请求失败的原因是您试图向其发出AJAX请求的远程域没有发送正确的CORS
标头。因此浏览器将阻止该请求。您应该与bulksms.icubetech.com
的管理员交谈,以便为您自己的域显式启用CORS。
这个问题显然与前端代码无关。您尝试访问的服务器应该允许来自其他域的请求。在您的情况下,bulksms.icubetech.com不允许来自域www.w3schools.com的CORS请求。
- 否'访问控制允许来源'标头存在于IISNOde中请求的资源(AngularJS+NodeJs)上
- "资源不足错误“;当从Chrome控制台重复发出ajax请求时
- ajax请求返回me“;错误404”;即使资源存在
- XMLHttpRequest无法加载.否'访问控制允许来源'标头存在于请求的资源上
- 将servlet请求从javascript发送到java给出了“;请求的资源不可用”;
- 否'访问控制允许来源'请求的资源上存在标头.-Pinterest oauth实现
- 否'访问控制允许来源'标头存在于请求的资源上.起源'http://localhost:8100&
- 否'访问控制允许来源'标头存在于请求的资源上.原点''因此不允许访问
- 否'访问控制允许来源'当跨域设置为true时,请求的资源错误中存在标头
- AngularJS:没有'访问控制允许来源'标头存在于请求的资源上
- 亚马逊s3 Javascript-否'访问控制允许来源'标头存在于请求的资源上
- 是否有任何事件在浏览器请求另一个资源(如图像)之前触发
- 在AngularJS$资源模块上请求之前获取URL
- Angular$http$资源等价于PDF GET请求
- Azure Blob 存储 请求的资源错误中不存在访问控制允许源标头
- Socket.io 请求的资源上不存在“访问控制允许源”标头.因此,不允许访问源“http://localhost”
- Windows上的CouchDB说请求的资源上不存在“Access-Control-Allow-Origin”标头
- 跨源资源错误:请求的资源上不存在“访问控制允许源”标头
- 区分对$http的应用程序调用和 Angular 对拦截器中静态资源的请求
- 什么会导致Firefox在执行外部脚本后多次请求资源