-2

i'm trying to fetch url through javascript but i got cors protected error and i can't able to figure out how should i fix

here's my code

<script>
 

fetch("https://www.nseindia.com/api/liveEquity-derivatives?index=nse50_fut",
  {
    mode: 'cors',
    credentials: 'include',
    method: 'GET',
    header: {
'Access-Control-Allow-Origin':'*',}
}).then(
    res => {
      res.json().then(
        data => {
        //   console.log(data.Algo);
          if (data.Algo.length > 10) {
  
            var temp = "";  
            data.Algo.forEach((itemData) => {
              temp +="<tr>";
              temp +="<td class=" + "col-md-8" + ">" + itemData.underlying + "</td>";
              temp += "<td class=" + "col-md-8" + ">" + itemData.identifier + "</td>";
              temp += "<td class=" + "col-md-8>" + itemData.instrumentType + "</td>";
              temp += "<td  class=" + "col-md-8" + ">" + itemData.contract + "</td></tr>";
      }
            );
            document.getElementById('data').innerHTML = temp;
          }

    
    
        }
      )
    }
  )</script>
James Z
  • 12,209
  • 10
  • 24
  • 44
I'm RamOP
  • 1
  • 2

1 Answers1

-1

How do I fix CORS header Access-Control allow Origin missing?

=>If the server is under your control, add the origin of the requesting site to the set of domains permitted access by adding it to the Access-Control-Allow-Origin header's value. You can also configure a site to allow any site to access it by using the correct domain. Example: Access-Control-Allow-Origin :"http://www.my-domain.example"

Access-Control-Allow-Origin : "*" => this is used for public APIs not Industrial or Personal APIs.

const url =
    "https://www.nseindia.com/api/liveEquity-derivatives?index=nse50_fut";

fetch(url, {
    mode: "cors",
    credentials: "include",
    method: "GET",
    headers: { "Access-Control-Allow-Origin": "*" },
}).then((res) => {
    res.json().then((data) => {
        console.log(data.Algo);

        /* 
        if (data.Algo.length > 10) {
            var temp = "";
            data.Algo.forEach((itemData) => {
                temp += "";
                temp += "" + itemData.underlying + "";
                temp += "" + itemData.identifier + "";
                temp += "" + itemData.instrumentType + "";
                temp += "" + itemData.contract + "";
            });
            document.getElementById("data").innerHTML = temp;
        }
        */
    });
});

There was a slight mistake in your code i have fixed it. Can try now if you have access to the domain replace the * with the actual domain origin

Tohirul Islam
  • 334
  • 1
  • 9