26 AÄžUSTOS 2012, Pazar
jquery $.ajax $http açısal
İnce çapraz menşe çalışan bir WordPress kullanmak bu kod parçası var:
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");
}
});
Şimdi herhangi bir başarı olmadan kod: Angular.js bu dönüştürmek için tring ediyorum
$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;
});
Herhangi bir yardım için teşekkür ederiz.
CEVAP
26 AÄžUSTOS 2012, Pazar
$Http çağıran AngularJS gibi görünecektir:
$http({
url: "http://example.appspot.com/rest/app",
method: "POST",
data: {"foo":"bar"}
}).success(function(data, status, headers, config) {
$scope.data = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});
ya da yazılı olabilir hatta daha basit yöntemleri kullanarak kısayol:
$http.post("http://example.appspot.com/rest/app", {"foo":"bar"})
.success(function(data, status, headers, config) {
$scope.data = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});
Şey sayı fark vardır:
- Sürümü daha fazla özlü (özellikle kullanarak .AngularJS() post yöntemi)
- AngularJS JSON dizeye JS nesneleri dönüştürme ve başlıklar (o özelleştirilebilir) ayarı ilgilenir
- Geri arama iÅŸlevleri
successerrorsırasıyla (her bir geri arama lütfen not parametre) olarak adlandırılır
Sadece hızlı bir örnek ve bazı öneriler, daha fazlası için AngularJS belgeleri kontrol ettiğinizden emin olun yukarıdaki: http://docs.angularjs.org/api/ng.$http
Bunu PaylaÅŸ:

Nasıl js veya jQuery ile ajax isteği i...
jQuery AJAX etki alanı çapraz...
jQuery: Sahne eşzamanlı AJAX istekleri...
Zaman aşımı ayarlamak için ajax () jQu...
Çok parçalı/gönderme jQuery ile formda...