Rewrite jQuery Ajax call in AngularJS

Is there an equivalent AngularJS call for this jQuery ajax POST with contentTypeand setRequestHeader?

$.ajax({
    url: "http://localhost/songs",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    beforeSend: function (xhr) {
        xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");
    },
    success: function(data){
        console.log(data);
    }
});
+5
source share
1 answer

You might want to use $ http or $ resource . In the $ http doc document, you can see the section Setting HTTP headers to set SOAPAction, and the default content type will be set to json already (although you should also override this).

This may get you started, and I would be interested to see other answers for a better way, because it seems limited.

var module = angular.module('myApp', []);

module.config(function ($httpProvider) {
    $httpProvider.defaults.headers.post['SOAPAction'] = 'http://schemas.microsoft.com/sharepoint/soap/UpdateListItems';
});

function myController($http) {
    var data = { 'value1': 1, 'value2': 2 };

    $http.post('/songs', data).success(function (data) {
        console.log(data);
    });
}

, - , change $resource, .

: , , , $http :

var data = { 'value1': 1, 'value2': 2 };

$http.post('/songs', data, {headers: {'SOAPActions': 'http://schemas.microsoft.com/sharepoint/soap/UpdateListItems'}})
    .success(function (data) {
        console.log(data);
    }); 
+11

All Articles