官方文档:webapi-tutorial
1、创建Site Settings
添加要使用表的site settings 两条
格式为:
Webapi/[表名]/enabled : 启用此表
Webapi/[表名]/fields: 要查找的字段
如:
Webapi/contact/enabled
Webapi/contact/fields
填写到name,website选择要用到这个的portal网站;
enabled中value填True
fields中value填要查的字段名,逗号隔开,或者查全部字段名填写“*”;
2、在自定义js中使用它
(function (webapi, $) { function safeAjax(ajaxOptions) { var deferredAjax = $.Deferred(); shell.getTokenDeferred().done(function (token) { // Add headers for ajax if (!ajaxOptions.headers) { $.extend(ajaxOptions, { headers: { "__RequestVerificationToken": token } }); } else { ajaxOptions.headers["__RequestVerificationToken"] = token; } }).fail(function () { deferredAjax.rejectWith(this, arguments); // On token failure pass the token ajax and args }); $.ajax(ajaxOptions) .done(function (data, textStatus, jqXHR) { //validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve); }).fail(deferredAjax.reject); //ajax return deferredAjax.promise(); } webapi.safeAjax = safeAjax; })(window.webapi = window.webapi || {}, jQuery) function Test(){ webapi.safeAjax({ type: "get", url: "/_api/contacts", contentType: "application/json", async: false, success: function (res, status, xhr) { console.log(res) }, error: function (res) { console.log("error", res); } }); }
在使用前可以先用portal网址+“/_api/contacts”,测试下是否正常
3、更新与新增
//更新
webapi.safeAjax({ type: "PATCH", url: "/_api/[表名](" + [主键id]+ ")", contentType: "application/json", async: false, data: JSON.stringify(recordObj), success: function (res) { console.log(res); }, error: function (ex) { console.log(ex); //alert("") } });
//新增
webapi.safeAjax({ type: "POST", url: "/_api/[表名]", contentType: "application/json", async: false, data: JSON.stringify(recordObj), success: function (res) { console.log(res); }, error: function (ex) { console.log(ex); //alert("") } });
标签:WebApi,function,console,log,res,App,webapi,safeAjax,Portal From: https://www.cnblogs.com/ctwpx/p/16582757.html