我知道有很多关于这个主题的文章,但遗憾的是没有一个解决方案适合我。
我使用apache 2.4(httpd)运行linux red hat 7.2。我正在作为localhost直接在服务器上工作。 API是基于python的,我几乎没有经验 - 我从github mozilla http observatory下载了程序。
我已经尝试了几个小时的不同设置,并且在这个问题上没有取得任何进展,现在正在寻求进一步的帮助。
我在POST之前的主要页面是http://localhost
这是js(ajax)
POST XHR调用:http://localhost:57001/api/v1/scan
function loadTLSObservatoryResults(rescan, initiateScanOnly) {
'use strict';
var rescan = typeof rescan !== 'undefined' ? rescan : false;
var initiateScanOnly = typeof initiateScanOnly !== 'undefined' ? initiateScanOnly : false;
/*var SCAN_URL = 'https://tls-observatory.services.mozilla.com/api/v1/scan';
var RESULTS_URL = 'https://tls-observatory.services.mozilla.com/api/v1/results';
var CERTIFICATE_URL = 'https://tls-observatory.services.mozilla.com/api/v1/certificate';*/
var SCAN_URL = 'http://localhost:57001/api/v1/scan';
var RESULTS_URL = 'http://localhost:57001/api/v1/results';
var CERTIFICATE_URL = 'http://localhost:57001/api/v1/certificate';
// if it's the first scan through, we need to do a post
if (Observatory.state.third_party.tlsobservatory.scan_id === undefined || rescan) {
// make a POST to initiate the scan
$.ajax({
data: {
rescan: rescan,
target: Observatory.hostname
},
initiateScanOnly: initiateScanOnly,
dataType: 'json',
method: 'POST',
error: function() { errorResults('Scanner unavailable', 'tlsobservatory') },
success: function (data) {
Observatory.state.third_party.tlsobservatory.scan_id = data.scan_id;
if (this.initiateScanOnly) { return; }
loadTLSObservatoryResults(); // retrieve the results
},
url: SCAN_URL
});
这是我的httpd.conf
基本上只包括这一行:
LoadModule headers_module modules/mod_headers.so
这是我的.htaccess文件:
RewriteEngine On
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
我已经重新启动了apache,但我在开发者控制台中看到了相同的警告:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:57001/api/v1/scan. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
答案 0 :(得分:0)
出于好奇,你在客户端使用像Ember或Angular这样的Javascript构建环境吗?因为这会影响您的托管端口,并可能导致与跨域相关的错误。
你可以做的另一件事是稍微修改你的ajax并创建一个帮助函数:
<?php
if(!empty($_POST["Province_id"])) {
$country_id =$_POST['country_id'];
if($country_id=="SelectProvince" || $country_id=="")
$country_id=$_POST['Province_id'];
$City_Name=$_POST['City_Name'];
$results = mysqli_query($con, "SELECT City_Name FROM location WHERE Province = '$country_id' ");
?>
<option value="SelectCity">Select City</option>
<?php
foreach($results as $state) {
?>
<option value="<?php echo $state["City_Name"]; ?>" <?=$state["City_Name"] == $City_Name ? ' selected="selected"' : '';?>><?php echo $state["City_Name"]; </option>
<?php
}
}
?>
试试这个,让我知道它是否适合你。