Javascript 使用jQuery的NetSuite Restlet
我正在尝试使用jQuery访问NetSuite restlet。以下是我的代码:Javascript 使用jQuery的NetSuite Restlet,javascript,jquery,netsuite,Javascript,Jquery,Netsuite,我正在尝试使用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: "appl
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中的“网络”选项卡时,它会给出以下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的文档,所以我在这里有点盲目。我应该只使用普通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);
});
更多信息:
基本上不会
尽管@adolfo garza的回答确实正确地显示了JSONP,但使用Restlet并没有任何好处,您放弃了一个永远不能用于敏感内容的登录。基本上,您已经在公共internet上发布了一个Netsuite凭据。这不会有什么好结果
这是Suitelet的一个用例。您可以创建一个具有公共访问权限的Suitelet(无需登录即可使用;访问所有角色),然后您就不需要身份验证(尽管如果您需要按客户筛选信息,可以通过购物会话或结帐会话身份验证)
如果您只是尝试测试一个真正的Restlet用例,那么您应该使用Node或一些非基于浏览器的应用程序来进行测试 因此,基本上使用类似于cURL的方法来获取数据,然后利用我们在站点内从NetSuite检索到的数据。对吗?谢谢。我将使用cURL来实现这一点。您是说在PHP中使用cURL从RestLet获取数据并将其显示为服务器端渲染吗?或者使用curl从命令行测试RestLet?我的意思是在PHP脚本中使用curl来传递数据。这是正确的,这就是为什么我将其标记为curl,但我选择curl是因为@bknights在下面的答案中提出了安全风险。是的,@bknights很好地抓住了这个问题。我全神贯注于你是否可以,我没有停下来想你是否应该。