Skip to content

CORS Policy issue #1136

@spsarola

Description

@spsarola

I want to make call from Shopify store to create a draft order with Remix API (kind of ajax I have set)

	async function sachin() {
	  console.log("Sending OTP TRYyy...");

	  const res = await fetch("https://d0870bb651c6.ngrok-free.app/api/create-draftorder", {
		method: "POST",
		headers: { "Content-Type": "application/json" },
		body: JSON.stringify({
		  shop: Shopify.shop, // Shopify global var
		  phone: "+91 9558656777",
		}),
	  });

	  const data = await res.json();
	  console.log("Response:", data);
	}

Get Error

Access to fetch at 'https://d0870bb651c6.ngrok-free.app/api/send-otp' from origin 'https://upsellmaxx-checkout-ext-test-build-store.myshopify.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

this is api.create-draftorder.jsx

	import { json } from "@remix-run/node";
	import { logToFile } from "../utils/logger.server";

	const corsHeaders = {
	  "Access-Control-Allow-Origin": "https://extensions.shopifycdn.com", // or "*" for any origin
	  "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
	  "Access-Control-Allow-Headers": "Content-Type, Authorization",
	  "Access-Control-Max-Age": "86400", // cache preflight for 24h
	};

	export async function loader({ request }) {
	  logToFile(`Loader called for shop`);
	  if (request.method === "OPTIONS") {
		// Preflight request
		return new Response(JSON.stringify({ ok: true, message: "Success", data: "analyticDbResponse" }), { status: 200, headers: corsHeaders });
	  }

	  const url = new URL(request.url);
	  const shop = url.searchParams.get("shop");

	  logToFile(`Loader called for shop: ${shop}`);

	  if (!shop) {
		return new Response(
		  JSON.stringify({ message: "Missing data. Required: shop" }),
		  { status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } }
		);
	  }

	  return new Response(
		JSON.stringify({ ok: true, message: "Success", data: "analyticDbResponse" }),
		{ status: 200, headers: { ...corsHeaders, "Content-Type": "application/json" } }
	  );
	}

	export async function action({ request }) {
	  logToFile(`Loader called for shop`);
	  if (request.method === "OPTIONS") {
		return new Response(JSON.stringify({ message: "Missing data: shop" }), { status: 200, headers: corsHeaders });
	  }

	  if (request.method === "POST") {
		const data = await request.json();

		if (!data.shop) {
		  return new Response(
			JSON.stringify({ message: "Missing data: shop" }),
			{ status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } }
		  );
		}

		// Do your DB logic here
		return new Response(
		  JSON.stringify({ message: "success" }),
		  { status: 200, headers: { ...corsHeaders, "Content-Type": "application/json" } }
		);
	  }

	  return new Response(
		JSON.stringify({ message: "Method not allowed" }),
		{ status: 405, headers: { ...corsHeaders, "Content-Type": "application/json" } }
	  );
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions