자바 스크립트를 사용하는 CORS에 문제가 있습니다.
Cross-Origin Request Blocked : 동일한 출처 정책이 http://openexchangerates.org/latest.json 에서 원격 리소스 읽기를 허용하지 않습니다 . (이유 : CORS 헤더 'Access-Control-Allow-Origin'누락).
이 문제를 해결하기 위해 laravel-cors 패키지를 설치 합니다.
하지만 전혀 도움이되지 않았습니다. 이 문제를 해결하는 방법을 조언 해 줄 수 있습니까? 문제가있는 곳과이 패키지가 작동하지 않는 이유를 확인하려면 어떻게 디버깅 할 수 있습니까?
이것은 내 코드입니다.
에 \Http\Kernel.php
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\Barryvdh\Cors\HandleCors::class,
];
에 \config\app.php
'providers' => [
Barryvdh\Cors\ServiceProvider::class,
],
에 \config\cors.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Laravel CORS
|--------------------------------------------------------------------------
|
| allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
| to accept any value.
|
*/
'supportsCredentials' => false,
'allowedOrigins' => ['*'],
'allowedHeaders' => ['*'],
'allowedMethods' => ['*'],
'exposedHeaders' => [],
'maxAge' => 0,
];
내 js 종료 :
$(document).ready(function(){
fx.base = "EUR";
fx.settings = {
from : "EUR"
};
var amount = 9.99; //in SolidShops, you could use: {{ product.price }}
// Load exchange rates data via the cross-domain/AJAX proxy:
$.getJSON(
'http://openexchangerates.org/latest.json',
function(data) {
// Check money.js has finished loading:
if ( typeof fx !== "undefined" && fx.rates ) {
fx.rates = data.rates;
fx.base = data.base;
} else {
// If not, apply to fxSetup global:
var fxSetup = {
rates : data.rates,
base : data.base
}
}
// now that we have exchange rates, add a few to our page
var USD = fx.convert(amount, {to: "USD"}); //13.22784197768393
var GBP = fx.convert(amount, {to: "GBP"}); //8.567532636985659
var JPY = fx.convert(amount, {to: "JPY"}); //1028.1670562349989
// we can now use the accounting.js library to format the numbers properly
USD = accounting.formatMoney(USD, "$ ", 2, ",", ".");
GBP = accounting.formatMoney(GBP, "£ ", 2, ",", ".");
JPY = accounting.formatMoney(JPY, "¥ ", 2, ",", ".");
$("ul.currencies").append("<li>USD estimate: " + USD + "</li>");
$("ul.currencies").append("<li>GBP estimate: " + GBP + "</li>");
$("ul.currencies").append("<li>JPY estimate: " + JPY + "</li>");
}
);
});
대신 URL https://openexchangerates.org/api/latest.json을 사용해야합니다.
질문에 인용 된 오류 메시지는 코드가 URL을 요청할 때 openexchangerates.org
서버가 Access-Control-Allow-Origin
응답 헤더를 코드로 다시 보내지 않음을 나타냅니다 http://openexchangerates.org/latest.json
.
따라서 자체 코드가 제공되는 자체 서버 백엔드에서 수행하는 CORS 구성은 중요하지 않습니다. 문제 openexchangerates.org
는 Access-Control-Allow-Origin
응답 헤더를 다시 보내지 않았기 때문입니다 .
그러나 대신 올바른 URL ( https://openexchangerates.org/api/latest.json )을 사용하면 해당 서버가 Access-Control-Allow-Origin
응답으로 헤더를 다시 전송 하고 "CORS 헤더 'Access-Control- Allow-Origin 'missing” 오류 메시지가 더 이상 표시됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다