0

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

sandhya
  • 5
  • 3

1 Answers1

0

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.

  • as mentioned in the above comment i have added NLAuth header but still it is not working can you suggest me any other way to resolve this issue – sandhya Mar 28 '23 at 06:53
  • The NLAuth header is not a permitted authentication method. You have to use TBA. Also, that is unrelated to the error you're getting. If you want to build a webpage hosted outside of NetSuite and call NetSuite API's you're going to have to do it using server-side programming. – Steffen Andersland Mar 28 '23 at 13:34