首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript在加载不同url时,请求的资源上不存在'Access-Control-Allow-Origin‘标头

Javascript在加载不同url时,请求的资源上不存在'Access-Control-Allow-Origin‘标头
EN

Stack Overflow用户
提问于 2016-01-09 15:24:23
回答 3查看 1.3K关注 0票数 1

我试图从不同的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.

代码语言:javascript
复制
<!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>
EN

回答 3

Stack Overflow用户

发布于 2016-01-09 15:33:43

您从http://bulksms.icubetech.com/api/checkbalance.php获得的响应应该在其报头中包含Access-Control-Allow-Origin

它应该是这样的:

代码语言:javascript
复制
Access-Control-Allow-Origin: yoursite.com

如果不满足上述要求,您将无法通过ajax访问此站点。

或者,您可以使用代理php脚本来完成此操作。这个想法是在您自己的域中获取一个php脚本,以便与您试图访问的站点进行对话,并给出结果。这不会引起任何跨域问题,因为您的浏览器只与您自己域中的php脚本通信。下面是一个代理脚本示例。请将其更改为适合您的需要。

代码语言:javascript
复制
<?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);

?>
票数 1
EN

Stack Overflow用户

发布于 2016-01-09 15:33:31

此请求失败的原因是您试图向其发出AJAX请求的远程域没有发送正确的CORS头。因此浏览器将阻止该请求。您应该与bulksms.icubetech.com的管理员联系,以便为您自己的域显式启用CORS。

票数 0
EN

Stack Overflow用户

发布于 2016-01-09 15:37:12

这个问题显然与你的前端代码无关。您尝试访问的服务器应允许来自其他域的请求。在您的情况下,bulksms.icubetech.com不允许来自域www.w3schools.com的CORS请求。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34690865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档