$(" #__ REQUESTDIGEST")。val()是未定义的 [英] $("#__REQUESTDIGEST").val() is Undefined

查看:98
本文介绍了$(" #__ REQUESTDIGEST")。val()是未定义的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 
var data = {
    __metadata:{'type':'SP.Data.SuggestionListItem'},
建议:'ABC',
标题:'https://www.facebook.com'
};

$ .ajax({
url:SiteUrl
+ '/ _ api / web / Lists / GetByTitle(\'suggestion \')/ items',
方法:" POST",
async:false,
header:{

" accept":" application / json; odata = verbose",
" content-type":" application / json; odata = verbose",
" ; X-RequestDigest":$(" #__ REQUESTDIGEST")。val(),
" IF-MATCH":" *",//覆盖sharepoint列表项
中的更改"X-HTTP-Method":"MERGE"。 //指定sharepoint列表中的更新操作
},
data:JSON.stringify(data),
success:function(data){
console.log(data);
},
错误:函数(错误){
alert("更新项时出错:" + JSON.stringify(err));
}
});



我正在使用上面的代码要将项目添加到简单的常规列表,它将返回此项


更新项时出错:{" readyState":4," responseText":""," responseXML" :null,"status":403,"statusText":"禁止"}


回复提醒。


放入 

  $(&#; REQUESTDIGEST")。val()  

并显示在警报中,因此未定义消息。 


我在一般的html html文件上测试它。


它在获取数据方面工作正常,但在帖子中面临问题。


List有两列Title和Suggestion只键入单行文本。

解决方案


尝试在SharePoint页面上使用以下REST。查看是否可以在列表中创建新项目。

 var siteUrl =" http:// sp / sites / team" 

var listName =" Suggestion";

var titleVal =" test";

var suggestionVal ='ABC';

var itemProperties = {

" __ metadata":{ "type":"SP.Data.SuggestionListItem"},

" Title" ;:titleVal,

" Suggestion":suggestionVal

};


.ajax({

url:siteUrl +" / _ api / web / lists / GetByTitle('" + listName +" ')/ items",

输入:" POST",

标题:{

" accept":" application / json; odata = verbose",

" X-RequestDigest":


(" #__ REQUESTDIGEST")。val(),

" content-Type":" application / json ;的OData =冗长"

},

数据:JSON.stringify(itemProperties),

成功:函数(数据){

的console.log(data.d.results);

},

错误:函数(错误){

alert(JSON.stringify(error));

}

});

}





SharePoint上的CRUD操作列表使用REST API:


https://www.c-sharpcorner .com / article / crud-operation-on-a-sharepoint-list-using-rest-api /


如果您尝试从外部(SharePoint外部)创建项目,则最好使用CSOM。


Best 问候,


琳达 张



var data = {
    __metadata: { 'type': 'SP.Data.SuggestionListItem' },
Suggestion:'ABC',
Title:'https://www.facebook.com'
};

$.ajax({ url: SiteUrl+'/_api/web/Lists/GetByTitle(\'Suggestion\')/items', method: "POST", async: false , headers: { "accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "IF-MATCH": "*", //Overrite the changes in the sharepoint list item "X-HTTP-Method": "MERGE" // Specifies the update operation in sharepoint list }, data: JSON.stringify( data), success: function (data) { console.log(data); }, error: function(err) { alert("Error while updating item: " + JSON.stringify(err)); } });

I am using above code to add item to a simple general list its return this 

Error while updating item: {"readyState":4,"responseText":"","responseXML":null,"status":403,"statusText":"Forbidden"}

In response alert.

I tested the above after putting 

 $("#__REQUESTDIGEST").val() 

in a variable and showed in alert so it's messaging undefined. 

I am testing it on a general html html file.

It was working fine in fetching data but facing issue in post.

List has two columns Title and Suggestion only type as single line text.

解决方案

Hi,

Try using the REST below on a SharePoint Page. See if you can create a new item in the list.

                var siteUrl = "http://sp/sites/team"

                var listName = "Suggestion";  

                var titleVal = "test";

                var suggestionVal = 'ABC';

                var itemProperties = {

                                "__metadata": { "type": "SP.Data.SuggestionListItem" },

                                "Title": titleVal,

                                "Suggestion": suggestionVal

                };


.ajax({ url: siteUrl + "/_api/web/lists/GetByTitle('"+listName+"')/items", type: "POST", headers: { "accept": "application/json;odata=verbose", "X-RequestDigest":


("#__REQUESTDIGEST").val(), "content-Type": "application/json;odata=verbose" }, data: JSON.stringify(itemProperties), success: function(data) { console.log(data.d.results); }, error: function(error) { alert(JSON.stringify(error)); } }); }


CRUD Operations On A SharePoint List Using REST API:

https://www.c-sharpcorner.com/article/crud-operation-on-a-sharepoint-list-using-rest-api/

If you are trying to create an item from external (outside of SharePoint), you’d better use CSOM.

Best regards,

Linda Zhang


这篇关于$(" #__ REQUESTDIGEST")。val()是未定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆