相关疑难解决方法(0)

从jquery $ .ajax到angular $ http

我有这个jQuery代码,可以很好地交叉起源:

jQuery.ajax({
    url: "http://example.appspot.com/rest/app",
    type: "POST",
    data: JSON.stringify({"foo":"bar"}),
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (response) {
        console.log("success");
    },
    error: function (response) {
        console.log("failed");
    }
});
Run Code Online (Sandbox Code Playgroud)

现在我想把它转换成Angular.js代码而没有任何成功:

$http({
    url: "http://example.appspot.com/rest/app",
    dataType: "json",
    method: "POST",
    data: JSON.stringify({"foo":"bar"}),
    headers: {
        "Content-Type": "application/json; charset=utf-8"
    }
}).success(function(response){
    $scope.response = response;
}).error(function(error){
    $scope.error = error;
});
Run Code Online (Sandbox Code Playgroud)

任何帮助赞赏.

ajax jquery cross-domain angularjs angular-http

121
推荐指数
1
解决办法
14万
查看次数

如果可能,我应该使用angularjs $ http服务请求或jquery ajax吗?

在我的项目中,我使用angularjs框架,并且$http每当我进行ajax调用时都喜欢使用该服务.但是在项目的某些部分,如果没有通过ajax调用直接更新UI,并且不需要angularjs绑定,我应该使用$httpservice还是plain jquery.ajax

更具体地说,我应该最小化我不关心UI的项目中的angularjs依赖关系,还是用angular服务和指令紧紧包裹整个项目?

javascript ajax jquery angularjs

4
推荐指数
1
解决办法
3678
查看次数