ALLOWED_ORIGINS=http://localhost:5173,http://localhost:3000,https://your-production-url.comแทน your-production-url.com ด้วย URL จริงของคุณ
- เปิด AWS Console → Lambda
- เลือก Function:
scamreport-api(หรือชื่อที่คุณตั้ง)
- คลิก Configuration tab
- เลือก Environment variables (ด้านซ้าย)
- คลิก Edit
- คลิก Add environment variable
- ใส่:
- Key:
ALLOWED_ORIGINS - Value:
http://localhost:5173,http://localhost:3000,https://your-production-url.com
- Key:
- คลิก Save
- Lambda จะ auto-reload environment variables
- ไม่ต้อง deploy code ใหม่
- ไปที่ API Gateway Console
- เลือก API ของคุณ
- เลือก CORS (ด้านซ้าย)
- ตั้งค่าดังนี้:
http://localhost:5173
http://localhost:3000
https://your-production-url.com
หรือถ้าต้องการใช้ Wildcard สำหรับ localhost:
http://localhost:*
https://your-production-url.com
GET,POST,PUT,DELETE,OPTIONS
Content-Type,Authorization,X-Requested-With
true
3600
- คลิก Save
- Deploy API → เลือก stage (prod/dev)
แทนที่ your-production-url.com ด้วย URL จริง เช่น:
https://scamreport.vercel.app
https://scamreport.netlify.app
https://d1234567890.cloudfront.net
https://scamreport.com
https://www.scamreport.com
ถ้ามีทั้ง www และ non-www ต้องใส่ทั้ง 2 อัน!
# Development
ALLOWED_ORIGINS=http://localhost:5173,http://localhost:3000,http://127.0.0.1:5173
# Development + Production
ALLOWED_ORIGINS=http://localhost:5173,http://localhost:3000,https://scamreport.vercel.app
# Production + www subdomain
ALLOWED_ORIGINS=https://scamreport.com,https://www.scamreport.com
# All (Development + Production + www)
ALLOWED_ORIGINS=http://localhost:5173,http://localhost:3000,https://scamreport.com,https://www.scamreport.com# Test OPTIONS (preflight)
curl -X OPTIONS https://your-api-gateway-url.com/table/complaints \
-H "Origin: http://localhost:5173" \
-H "Access-Control-Request-Method: GET" \
-v
# ควรได้ response:
# Access-Control-Allow-Origin: http://localhost:5173// เปิด Developer Console แล้วรัน:
fetch('https://your-api-gateway-url.com/table/complaints', {
headers: {
'Origin': 'http://localhost:5173'
}
})
.then(r => r.json())
.then(console.log)
.catch(console.error);
// ไม่ควรมี CORS error// ใน React app
useEffect(() => {
fetch('https://your-api-gateway-url.com/table/complaints')
.then(r => r.json())
.then(data => console.log('✅ CORS OK:', data))
.catch(err => console.error('❌ CORS Error:', err));
}, []);เช็คตามลำดับ:
-
ตรวจสอบ Environment Variable
# ใน Lambda Console → Configuration → Environment variables # ต้องมี ALLOWED_ORIGINS และไม่มี space หลัง comma
-
ตรวจสอบ API Gateway CORS
- ต้อง Deploy API หลังตั้งค่า CORS
- ตรวจสอบว่าตั้งค่าที่ stage ที่ใช้งาน (prod/dev)
-
ตรวจสอบ URL ตรงกันทุก character
❌ https://example.com/ (มี trailing slash) ✅ https://example.com (ไม่มี trailing slash) ❌ http://example.com (http) ✅ https://example.com (https) -
ลอง Clear Cache
- Browser cache
- CloudFront cache (ถ้ามี)
- API Gateway cache
-
ดู Lambda Logs
# CloudWatch Logs → Log groups → /aws/lambda/your-function # จะเห็น origin ที่ request เข้ามา
# ใช้เฉพาะ production URL
ALLOWED_ORIGINS=https://scamreport.com,https://www.scamreport.com# เพิ่ม localhost
ALLOWED_ORIGINS=http://localhost:5173,http://localhost:3000,https://scamreport.com# ❌ อย่าใช้ wildcard ใน production!
ALLOWED_ORIGINS=*- ✅ ตั้งค่า
ALLOWED_ORIGINSใน Lambda - ✅ ตั้งค่า CORS ใน API Gateway (ถ้ามี)
- ✅ Deploy API
- ✅ Test จาก localhost
- ✅ Test จาก production URL
- ✅ Remove wildcard
*ถ้ามี
Last Updated: 2025-11-13 Status: Ready to deploy