CORS通过jQuery和Angular

时间:2016-06-28 15:05:21

标签: jquery angularjs cors

当我尝试使用JQuery创建跨域请求时,它工作正常,但是当我开始使用Angular时,它不起作用。

JQuery的:

var DataForAdminNewsletter = JSON.stringify({
    "key": "********************************",
    "subject": "Home Page",
    "emailTo": "********************",
    "emailFrom": "***********************",
    "html": 'Done'
});


$.ajax({
    type: "POST",
    url: "http://my.azurewebsites.net/mail.php",
    cache: false,
    data: { "req": DataForAdminNewsletter },

}).done(function (response) {

    alert("done");

});

角:

app.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
}
]);

app.controller("emailCtrl", function ($scope, $http) {

    var dataForAdminNewsletter = angular.toJson({
        "key": "*****************************",
        "subject": "New Email",
        "emailTo": "**************************",
        "emailFrom": "************************",
        "html": 'You have a new subscriber'

    });

    $scope.sendPost = function () {

        $http({
            url: 'http://my.azurewebsites.net/mail.php',
            method: "POST",
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            },
            data: { "req": dataForAdminNewsletter},
            crossDomain: true
        });

    }
});

我认为问题可能是对象,我发送到服务器端(黄色),因为这些对象具有不同的结构:

JQuery:http://screencast.com/t/HfuunCuLrcg), Angular:http://screencast.com/t/GldgpQI2Bhal

有人可以解释一下我做错了吗?

谢谢。

0 个答案:

没有答案