How can I avoid CORS errors when calling a NetSuit...
# ask-stanley-ai
s
How can I avoid CORS errors when calling a NetSuite Suitelet or RESTlet from a React app rendering inside netsuite?
c
AI generated answer (using internal Slack threads):
To avoid CORS errors when calling a NetSuite Suitelet or RESTlet from a React app rendering inside NetSuite, you can follow these steps: 1. Use the internal URL of the RESTlet instead of the external URL. The internal URL is appropriate for use with an internal Suitelet URL because they are on the same domain. This can help to avoid CORS issues. 2. When making the call from your React app, use the standard HTTPS GET and POST calls. You may need to include some authentication headers in the call. 3. If you're using Angular, you can use the Angular HTTP client. This client should work without any authentication setting as it should use your current user session. Here is a sample code snippet for making a call from a client-side script:
Copy code
javascript
fetch('https://${AccountId}.<http://app.netsuite.com/app/site/hosting/restlet.nl?script=1818&deploy=1')|app.netsuite.com/app/site/hosting/restlet.nl?script=1818&deploy=1')>
    .then(response => {
            console.log("response", response);
            return response.json()
        }
    )
    .then(data => console.log(data));
Please note that this code is using the Fetch API, which is a browser global. If you're running this code server-side, you might get a
ReferenceError: fetch is not defined
error. In that case, you might want to use the Angular HTTP client or another HTTP client that works server-side. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.869617045
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.861304879
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.856062651
Please provide your feedback on this answer.