In a SharePoint hosted App am trying to save data to my list in Office365 using REST API below is the code. The problem with this code is it works fine for less values passed (eg: my in my item
i pass "FirstName":window.Rest.operations.getValue('FirstName')
and "LastName":window.Rest.operations.getValue('LastName')
in this case data is saved to list), but if i pass more values in my item
(as below in code) to my list i get error Failed to load resource: the server responded with a status of 400 (Bad Request)
.Has anyone faced this problem, please help me out
`saveDetails:function(){
var hostwebUrl = window.Rest.operations.getHostWeb();
var appwebUrl = window.Rest.operations.getAppWeb();
var listName ='EmployeeDetails';
var url = appwebUrl+"/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + listName + "')/items?@target='"+hostwebUrl+"'";
var item = {"__metadata": { "type": "SP.Data.EmployeeDetailsListItem" },
"FirstName":window.Rest.operations.getValue('FirstName'),//This returns me string value
"LastName": window.Rest.operations.getValue('LastName'),
"EmailAddress": window.Rest.operations.getValue('EmailAddress'),
"Website": window.Rest.operations.getValue('Website'), //This is URL type field
"Position": window.Rest.operations.getValue('Position'),
"BirthDate": window.Rest.operations.getValue('BirthDate'),
"Phone": window.Rest.operations.getValue('Phone'),
"Fax": window.Rest.operations.getValue('Fax'),
"Relocate": window.Rest.operations.getValue('Relocate'),
"PreviousCompany": window.Rest.operations.getValue('PreviousCompany'),
"Reference": window.Rest.operations.getValue('Reference')
};
$.ajax({
url: url,
type: "POST",
data:JSON.stringify(item),
headers: { "accept": "application/json;odata=verbose;",
"content-type": "application/json;odata=verbose;",
"content-length":item.length,
"X-RequestDigest": $("#__REQUESTDIGEST").val() }, // return data format
success: function (data) {
alert('Success')
// alert("Found " + data.d.results.length + " items." + JSON.stringify(data));
},
error: function (data) {
$("#message").html("Failed to read items.");
}
});
},`
0 commentaires:
Enregistrer un commentaire