我有一个执行http POST请求的函数.代码如下所示.这很好用.
$http({
url: user.update_path,
method: "POST",
data: {user_id: user.id, draft: true}
});
Run Code Online (Sandbox Code Playgroud)
我有另一个http GET函数,我想发送数据到该请求.但我没有这个选择.
$http({
url: user.details_path,
method: "GET",
data: {user_id: user.id}
});
Run Code Online (Sandbox Code Playgroud)
的语法http.get
是
get(url,config)
有人可以帮我弄这个吗?
我正在尝试设置AngularJS与跨源资源进行通信,其中传递我的模板文件的资产主机位于不同的域上,因此角度执行的XHR请求必须是跨域的.我已经为我的服务器添加了适当的CORS标头,以便使其工作,但它似乎不起作用.问题是,当我在浏览器(chrome)中检查HTTP请求时,发送到资产文件的请求是OPTIONS请求(它应该是GET请求).
我不确定这是AngularJS中的错误还是我需要配置一些东西.根据我的理解,XHR包装器无法发出OPTIONS HTTP请求,因此看起来浏览器正在试图确定在执行GET请求之前是否"允许"首先下载资产.如果是这种情况,那么我是否还需要使用资产主机设置CORS标头(Access-Control-Allow-Origin:http://asset.host ..)?
AngularJS文档有关于$http
success
和error
方法的弃用通知.这个抽象是从库中删除的具体原因吗?
我可以设置context
在Angularjs $http
,就像我们可以做到这一点jQuery's $.ajax
?
define([
'app'
], function(app) {
app.controller("controller1", function($scope, $route, $http) {
return $http({
method: 'GET',
url: 'server.php'
}).then(function(response) {
$scope.contacts = response.data;
});
});
});
Run Code Online (Sandbox Code Playgroud)
另外,jQuery中还有更多的回调$.ajax
,比如.done
,.promise
我可以用它来操作context
下面这样的,我想知道我是否可以这样做Angularjs
?
$.ajax({
type: "GET",
dataType: "HTML",
url: 'server.php',
context: $("#container"),
async: true,
beforeSend: function() {
$(this).html('loading...');
},
success: function (returndata, status, jqxhr) {
$(this).html(returndata).hide().fadeIn();
},
}).fail(function() {
alert("error");
}).done(function(returndata) {
},
.always(function() {
alert("complete");
}
});
Run Code Online (Sandbox Code Playgroud) 继续我的移动应用程序开发学习过程,我发现了一个新的障碍:跨源请求共享或CORS.
我正在使用AngularJS + jQuery Mobile(Cordova手机客户端)和ASP.NET Web API(后端)的组合.我的问题是我无法完成对API控制器的POST请求(或任何其他类型的请求).
我的AngularJS控制器使用$http.post()
服务方法来调用Web API控制器.但是,Chrome调试器表示OPTIONS请求中的调用失败(可能是CORS预检请求).
我已经从以下帖子实现了CORS操作选择器:在Web API项目中启用CORS.即使很难,我也可以从Fiddler调用api方法,AngularJS一直在OPTIONS预检请求上失败.
关于AngularJS和跨域调用,我应该注意什么?我困境的任何可能解决方案?
谢谢.
在我的项目中,我使用angularjs框架,并且$http
每当我进行ajax调用时都喜欢使用该服务.但是在项目的某些部分,如果没有通过ajax调用直接更新UI,并且不需要angularjs绑定,我应该使用$http
service还是plain jquery.ajax
?
更具体地说,我应该最小化我不关心UI的项目中的angularjs依赖关系,还是用angular服务和指令紧紧包裹整个项目?
我正在使用AngularJS,无法找到解决此问题的方法:
我的控制器有一部分:
$scope.$on('showMoreNotifications', function (event) {
$.ajax({
url: '/notifications',
data: {
notificationCount: 30
},
success: function (e) {
$scope.notifications = e.Messages;
}
});
});
Run Code Online (Sandbox Code Playgroud)
这是使用此控制器的html:
<div class="widget" id="widget-notifications" ng-controller="NotificationsCtrl">
<span class="title" ng-click="$parent.$broadcast('showMoreNotifications')">@*showMoreNotifications()*@
Notifikace
</span>
<div class="messages">
<div ng-repeat="item in notifications" class="message-item type-{{item.Entity}}" data-id="{{item.AuditLogId}}">
<span class="type"></span>
<div class="description">
<span class="date">{{item.Date}}</span> / {{item.Message}}
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
如果我点击顶部的span类标题,控制器右键调用服务器并接收JSON数据.不幸的是,不要刷新与之相关的html.当我第二次点击时,html刷新第一次请求的数据.
我已经试图搜索anwser ...我被卡住了.
我尝试在控制器之间进行通信(http://onehungrymind.com/angularjs-communicating-between-controllers/).
这对我来说很好.
在下一步中,我尝试添加一个ajax请求,结果应该发送给控制器.
该请求做他的工作,但不幸的是只在每第二请求.
AJAX请求
var request = $.post("http://www.mydomain.com/search.php", { data: "" });
request.done(function( data ) {
sharedService.prepForBroadcast(data);
});
};
Run Code Online (Sandbox Code Playgroud)
这有什么问题?
JAVASCRIPT
var myModule = angular.module('myModule', []);
myModule.factory('mySharedService', function($rootScope) {
var sharedService = {};
sharedService.message = '';
sharedService.prepForBroadcast = function(msg) {
this.message = msg;
this.broadcastItem();
};
sharedService.broadcastItem = function() {
$rootScope.$broadcast('handleBroadcast');
};
return sharedService;
});
function Controller($scope, sharedService) {
$scope.handleClick = function() {
var request = $.post("http://www.mydomain.com/search.php", { data: "" });
request.done(function( data ) …
Run Code Online (Sandbox Code Playgroud) angularjs ×8
ajax ×3
javascript ×3
jquery ×3
controller ×1
cors ×1
cross-domain ×1
preflight ×1