gpt4 book ai didi

Javascript 加载不同 url 时请求的资源上不存在 'Access-Control-Allow-Origin' header

转载 作者:行者123 更新时间:2023-11-27 23:29:29 25 4
gpt4 key购买 nike

我正在尝试将值从不同的 url 加载到 div,加载时会出现错误。我尝试用谷歌搜索很多。我没有找到修复方法。我知道这个问题已经被问过,但仍然没有解决我的问题。

XMLHttpRequest 无法加载 http://bulksms.icubetech.com/api/checkbalance.php?user=&pass=。请求的资源上不存在“Access-Control-Allow-Origin” header 。因此,不允许访问来源“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>

最佳答案

您从 http://bulksms.icubetech.com/api/checkbalance.php 获得的响应应在其 header 中包含 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);

?>

关于Javascript 加载不同 url 时请求的资源上不存在 'Access-Control-Allow-Origin' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34690865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com