我正在尝试使用 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 中的“网络”选项卡时,它给出以下 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);
});
关于javascript - 使用 jQuery 的 NetSuite ReSTLet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42958102/