Mark Lalich

Mark Lalich

  • NA
  • 14
  • 922

Erros when adding to SP list through AngularJS.

Dec 28 2018 10:46 AM
I'm just trying to submit a simple item to a sharepoint online list.  Here's the code I have to do this.
 
  1. app.factory("organizationsSrvc", ['$rootScope''$http',  
  2.     function($rootScope, $http) {  
  3.         var listName = "Organizations";  
  4.   
  5.         var organizationsService = {};  
  6.   
  7.         //Organization operations  
  8.         var organizations = null;  
  9.   
  10.         organizationsService.add = function(organization) {  
  11.             var data = JSON.stringify(organization);  
  12.             data = data.replace(/[{}]/g, '');  
  13.             var datavalue = "{__metadata:{'type':'SP.Data.'" + listName + "'.ListItem'}," + data + "}";  
  14.             console.log(datavalue);  
  15.             $http({  
  16.                 url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('" + listName + "')/items",  
  17.                 method: 'POST',  
  18.                 headers: {  
  19.                     "Accept""application/json;odata=verbose",  
  20.                     "Content-Type""application/json;odata=verbose",  
  21.                     "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  22.                     "X-HTTP-Method""POST"  
  23.                 },  
  24.                 data: datavalue  
  25.             }).then(function success(response) {  
  26.                 console.log("[add Organization] POST worked");  
  27.                 console.log("[add Organization] New Id = " + response.Id);  
  28.                 debugger;  
  29.             }, function error(response) {  
  30.                 console.log("[add Organization] Error = " + response);  
  31.                 debugger;  
  32.             })  
  33.             console.log($http);  
  34.             debugger;  
  35.         };  
  36.   
  37.         return organizationsService;  
  38. }]);  
 When running this I get the following output in console.
 
 
{__metadata:{'type':'SP.Data.'Organizations'.ListItem'},"title":"test"}
 
organizations.controller.js:69 ƒ $http(requestConfig) {
if (!angular.isObject(requestConfig)) {
throw minErr('$http')('badreq', 'Http request configuration must be an object. Received: {0}', requestConfig);
 
POST ....... 400 
 
From what I'm seeing in this output, it looks like It's outputting line 14, then line 33, then hits the error on line 31.  It's saying it's a 400 error.
 
 Any help is appreciated!!

Answers (1)