NetSuite Restlet 使用 jQuery
NetSuite Restlet Using jQuery
我正在尝试使用 jQuery 访问 NetSuite restlet。这是我的代码:
jQuery.ajax({
url: "https://rest.na2.netsuite.com/app/site/hosting/restlet.nl?script=270&deploy=1&searchId=customsearch_active_models",
type: "GET",
dataType: "json",
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);
});
当我检查 Chrome/FF 中的 "Network" 选项卡时,它给我以下 401 响应:
XMLHttpRequest cannot load https://rest.na2.netsuite.com/app/site/hosting/restlet.nl?script=270&deploy=1&searchId=customsearch_active_models. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.tracksandtires.com' is therefore not allowed access. The response had HTTP status code 401.
我没有正确格式化授权部分吗?我找不到任何关于通过 jQuery 访问 NetSuite Restlet 的文档,所以我在这里有点盲目。我应该只使用 vanilla javascript 而不是 jQuery 吗?任何帮助将不胜感激!
尝试像这样使用 jsonp:
jQuery.ajax({
url: "https://rest.na2.netsuite.com/app/site/hosting/restlet.nl?script=270&deploy=1&searchId=customsearch_active_models",
type: "GET",
crossDomain: true,
dataType: "jsonp",
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);
});
更多信息:
How does Access-Control-Allow-Origin header work?
基本不会
尽管@adolfo-garza 的回答确实正确地显示了 JSONP,但是使用 Restlet 没有任何好处,而且您放弃了永远不能用于敏感内容的登录。基本上,您已经在 public 互联网上发布了您的 Netsuite 凭据之一。这不会有什么好结果。
这是 Suitelets 的用例之一。您创建了一个具有 public 访问权限的 Suitelet(无需登录即可使用;观众所有角色),然后您不需要身份验证(尽管如果您需要按客户过滤信息,可以通过购物会话或结帐会话身份验证的方法).
如果您只是想测试一个真正的 Restlet 用例,那么您应该使用 Node 或一些非基于浏览器的应用程序来执行此操作。
我正在尝试使用 jQuery 访问 NetSuite restlet。这是我的代码:
jQuery.ajax({
url: "https://rest.na2.netsuite.com/app/site/hosting/restlet.nl?script=270&deploy=1&searchId=customsearch_active_models",
type: "GET",
dataType: "json",
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);
});
当我检查 Chrome/FF 中的 "Network" 选项卡时,它给我以下 401 响应:
XMLHttpRequest cannot load https://rest.na2.netsuite.com/app/site/hosting/restlet.nl?script=270&deploy=1&searchId=customsearch_active_models. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.tracksandtires.com' is therefore not allowed access. The response had HTTP status code 401.
我没有正确格式化授权部分吗?我找不到任何关于通过 jQuery 访问 NetSuite Restlet 的文档,所以我在这里有点盲目。我应该只使用 vanilla javascript 而不是 jQuery 吗?任何帮助将不胜感激!
尝试像这样使用 jsonp:
jQuery.ajax({
url: "https://rest.na2.netsuite.com/app/site/hosting/restlet.nl?script=270&deploy=1&searchId=customsearch_active_models",
type: "GET",
crossDomain: true,
dataType: "jsonp",
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);
});
更多信息: How does Access-Control-Allow-Origin header work?
基本不会
尽管@adolfo-garza 的回答确实正确地显示了 JSONP,但是使用 Restlet 没有任何好处,而且您放弃了永远不能用于敏感内容的登录。基本上,您已经在 public 互联网上发布了您的 Netsuite 凭据之一。这不会有什么好结果。
这是 Suitelets 的用例之一。您创建了一个具有 public 访问权限的 Suitelet(无需登录即可使用;观众所有角色),然后您不需要身份验证(尽管如果您需要按客户过滤信息,可以通过购物会话或结帐会话身份验证的方法).
如果您只是想测试一个真正的 Restlet 用例,那么您应该使用 Node 或一些非基于浏览器的应用程序来执行此操作。