javascript - 使用 jQuery 的 NetSuite ReSTLet

标签 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: "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);
});

更多信息: How does Access-Control-Allow-Origin header work?

关于javascript - 使用 jQuery 的 NetSuite ReSTLet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42958102/

相关文章:

javascript - 如果我将一个 JavaScript 事件绑定(bind)到一个元素,然后删除该元素,该事件会发生什么?

javascript - 比较两个数组,缺失元素加0

javascript - 使滑动过渡在两个方向上无限

javascript - 如何设置链接的动态ID?

jquery - 滑动切换和改变字形

带有文本字段输入的 NetSuite 对话框 (SuiteScript 2.0)

javascript - 在 Angular Seed 项目中,index-async.html 有什么意义?

javascript - jQuery tablesorter 插件列宽在 IE7 中不正确

netsuite - Suitescript - 如何访问 "Related Records"子选项卡并获取/存储其关联值?

netsuite - 获取子列表行数据