Javascript No';访问控制允许原点';加载不同url时,请求的资源上存在标头
我试图将值从不同的url加载到一个div,当加载时,它会给我一个错误。我试着用谷歌搜索了很多。我没有找到解决办法。我知道这个问题已经被问到了,但仍然没有解决我的问题Javascript No';访问控制允许原点';加载不同url时,请求的资源上存在标头,javascript,jquery,Javascript,Jquery,我试图将值从不同的url加载到一个div,当加载时,它会给我一个错误。我试着用谷歌搜索了很多。我没有找到解决办法。我知道这个问题已经被问到了,但仍然没有解决我的问题 XMLHttpRequest无法加载http://bulksms.icubetech.com/api/checkbalance.php?user=&pass=. 请求的资源上不存在“Access Control Allow Origin”标头。起源'http://www.w3schools.com因此,不允许访问。 <!DOC
XMLHttpRequest无法加载http://bulksms.icubetech.com/api/checkbalance.php?user=&pass=. 请求的资源上不存在“Access Control Allow Origin”标头。起源'http://www.w3schools.com因此,不允许访问。
<!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>
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
$(“#div1”)。加载(“http://bulksms.icubetech.com/api/checkbalance.php?user=&pass=");
});
});
让jQuery AJAX更改此文本
获取外部内容
此请求失败的原因是您尝试向其发出AJAX请求的远程域没有发送正确的头。因此浏览器将阻止该请求。您应该与bulksms.icubetech.com的管理员联系,明确为您自己的域启用CORS。您从http://bulksms.icubetech.com/api/checkbalance.php
应在其标题中包含访问控制允许原点
应该是这样的:
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);
?>
这个问题显然与前端代码无关。
您尝试访问的服务器应允许来自其他域的请求。
在您的情况下,bulksms.icubetech.com不允许来自域www.w3schools.com的CORS请求。您是在本地主机还是外部服务器上运行此页面?在localhost和我在w3school上都尝试过,这两个地方给了我相同的请求error@MariusBalaj,这不会改变任何事情:-)@Shaik,这是因为http://bulksms.icubetech.com/api/
server不允许跨源请求,您可以通过调用jQuery.load()
方法从它们的一个页面外部执行此操作。唯一的解决方案是:1更改服务器配置以允许此类请求(如果不是您的服务器,则不容易),2使用代理服务器。如@kaido所说:Google如何使用Apache设置反向代理(假设您的应用程序托管在那里)感谢您的回复,但我不知道代理服务器vl检查您是否可以帮助更多使用fullno,它不发送头,但我非常确定它确实支持它。从客户端的角度来看,这相当于:-),但感谢您的挑剔。不,否则你的建议根本不可能。php不在我的服务器中,它是第三方。你试图访问的网站应该发送通过安全性所需的标题,或者你应该能够通过服务器脚本绕过它。你不能让奇迹发生在你的网站周围。