Sorunun PHP kodunu sormadığını biliyorum. Bu ön-olay ve Angular2 yüzünden buraya geldim. Matteo'nun cevabına baktığımda, sunucumun isteğinin neden 401 ile geri geldiğini anladım. Bunun nedeni, ön kontrol tarayıcısında Yetkilendirme belirtecini göndermemesi ve bu nedenle sunucunun 401 ile geri gelmesi ve tarayıcının CORS'e izin verilmemesi olduğunu düşünüyor. . Lütfen aşağıdaki kodun ne yaptığınızı bilmediğiniz sürece yalnızca geliştirme sunucusunda kullanılması gerektiğini unutmayın.
PHP bunu yapabilirsiniz:
if (strtolower($_SERVER['REQUEST_METHOD']) === 'options') {
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Range, Content-Disposition, Content-Type, Authorization');
header('Access-Control-Allow-Credentials: true');
echo 'Allowed';
exit;
}
VEYA Nginx
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
diğer türlü sen eklenmesi gerekir sunucuya gönderiyoruz başlıkları Hatırla-Access-Kontrol Allow-Başlıkları liste. Ben API ön uç ve nodeJS (Expressjs) için Eğik 2 ile çalışıyorum
"ASP.NET Web API'sinde Çapraz Kaynaklı İstekleri Etkinleştirme" için dokümanlar okudum ve yanıtta bu üstbilgiye sahip olmalısınız. Bu sayfada "CORS Nasıl Çalışır" bölümüne bakınız: http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api#how-it-works. –
*** Kapanış yapıyorum, çünkü bu sadece OP başına bir yazım hatasıdır. *** – Win
Bu sorunun sunucu özelliklerinde olduğunu düşünüyorum: http://stackoverflow.com/questions/36825429/angular-2-no -access-control-allow-origin-header-is-on-the-present-on-the-the-istekte –