在AngularJS中重写jQuery Ajax调用

pht*_*ven 5 ajax jquery angularjs

是否有一个AngularJS等效调用此jQuery ajax POST,用contentTypesetRequestHeader

$.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);
    }
});
Run Code Online (Sandbox Code Playgroud)

Glo*_*opy 11

您可能想要使用$ http$ resource服务.在$ http文档中,您可以看到设置HTTP标头的部分以设置SOAPAction,默认内容类型将设置为json(尽管您也应该能够覆盖它).

这可能会让你开始,我有兴趣看到更好的方式的其他答案,因为这似乎有限.

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);
    });
}
Run Code Online (Sandbox Code Playgroud)

基于这个线程,我不相信你可以为每个调用设置不同的标题,但看起来可能会改变允许它的$ 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);
    }); 
Run Code Online (Sandbox Code Playgroud)