SORU
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 success error sı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ş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Chaoticmoogle

    Chaoticmoogl

    13 ŞUBAT 2006
  • HuskyStarcraft

    HuskyStarcra

    4 HAZİRAN 2009
  • World Science Festival

    World Scienc

    1 Mayıs 2008