0

I created an Sharepoint-hosted application with angularjs.

And ran into a problem, my post request returns status -1, but saves the data. How to fix it?

My Lists posted on SPHostUrl.

Here is my base post service

     var postRequest = function (data, url) {
        var deferred = $q.defer();
        $http({
            url: baseUrl + url,
            method: "POST",
            headers: {
                "accept": "application/json;odata=verbose",
                "X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
                "content-Type": "application/json;odata=verbose"
            },
            data: JSON.stringify(data)
        }).then(function (result) {
            deferred.resolve(result);
        }, function (result) {
            deferred.reject(result.status);
        });
        return deferred.promise;
    };

And here is post service

    var addNew = function (certificate) {
        var data = {
            __metadata: { 'type': 'SP.Data.CertificatesListItem' },
            FullName: certificate.FullName,
            TypeOfCertificateId: certificate.TypeOfCertificateId,
            Validity: certificate.Validity
        };
        var url = '/_api/SP.AppContextSite(@target)/web/lists' + "/GetByTitle('Certificates')/Items?@target='" + window._spApplicationInfo.SPHostUrl + "'";
        return baseService.postRequest(data, url);
    };
0

It was my fault.

I used simple div's and button without any type.

My solution is to add $event.preventDefault():

template.html

<button class="btn btn-warning" data-ng-click="addTypeOfCertificate(typeOfCertificate, $event)">Сохранить</button>

and addTypeOfCertificate function:

        $scope.addTypeOfCertificate = function (typeOfCertificate, $event) {

           $event.preventDefault();
           save();
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.