I am trying to post form data to NetSuite restlet using jQuery ajax. Using below code to post data to NetSuite restlet OAuth1.0.
jQuery(".form-button").click(function(){
jQuery.ajax({
url: "https://tstdXXXXXX.restlets.api.netsuite.com/app/site/hosting/restlet.nl?
script=XXXX&deploy=1",
type: "POST",
crossDomain: true,
dataType: "jsonp",
mode: 'cors',
contentType: "application/json",
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "NLAuth nlauth_account=ACCOUNT#,
nlauth_email=EMAIL, nlauth_signature=XXXXXX, nlauth_role=ROLE#")
}
})
.done(function(data){
console.log(data);
});
});
after using above code initially I got error "http://localhost:8080’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present" . To fix this i have added mode "cors". Now I am getting ERR_ABORTED 401.
Thanks in advance
Firstly, you can't use the old NLAuth authentication method anymore as it's no longer supported and will have to use token based authentication. https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4623992425.html
Secondly, you're having a CORS issue, this isn't something specific to NetSuite. The TLDR is that CORS is a security feature that your browser is enforcing and to get around this you will have to send the http request from a server. https://auth0.com/blog/cors-tutorial-a-guide-to-cross-origin-resource-sharing/
I'm not sure how you plan on deploying this webpage you're making, but often times you will need to use a runtime like nodejs, php or python to host the webpage and handle API requests.