2016-01-09 17 views
1

我想從不同的URL加載值到一個div,當它被加載它給我一個錯誤。我試圖谷歌很多。我沒有找到解決辦法。我知道這個問題已經提出,但仍然沒有解決我的問題。Javascript No'Access-Control-Allow-Origin'標題存在於請求的資源上加載不同的URL

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> 
+0

你是在本地主機還是外部服務器上運行這個頁面? –

+0

在本地主機和我試圖在w3school這兩個地方給了我同樣的錯誤 – Shaik

+1

@MariusBalaj,這將不會改變任何:-) @Shaik,這是因爲'http://bulksms.icubetech.com/ api /'服務器不允許跨域請求,您通過從其頁面之外調用'jQuery.load()'方法來完成。唯一的解決方案是:** 1 **更改服務器配置,以便它允許這樣的請求(不容易,如果它不是您的服務器),** 2 **使用代理服務器。 – Kaiido

回答

0

爲什麼申請失敗的原因是遠程域你正試圖使一個AJAX請求,不發送正確CORS頭。所以瀏覽器會阻止這個請求。您應該與bulksms.icubetech.com的管理員通話以明確地爲您自己的域啓用CORS。

+0

「不支持」? – Kaiido

+0

是的,否則AJAX請求會起作用。 –

+0

不,它不**發送頭,但我很確定它支持它。 – Kaiido

1

您從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); 

?> 
+0

把php不在我的服務器,這是第三方 – Shaik

+0

您試圖訪問的網站應發送所需的頭,以通過安全或您應該能夠繞過這通過你的服務器腳本。你無法讓奇蹟發生在你的網站周圍。 –

0

這個問題顯然與您的前端代碼無關。 您嘗試訪問的服務器應允許來自其他域的請求。 就你的情況bulksms.icubetech.com不允許來自域www.w3schools.com的CORS請求。

相關問題