簡體   English   中英

Angular,將json發送到API

[英]Angular, sending json to an API

我正在嘗試將json發布到api,並給我以下錯誤...

http://www.website.com/getPriceNoAuth?json=[object%20Object] 405(不允許使用方法)

這是我嘗試發送的json對象,它的http resuest ...

var productAttributes = {
    "CostRequirements":[
        {
            "OriginPostcode":"BR60ND",
            "BearerSize":100,
            "BandwidthRequired":10,
            "ProductCode":"CON-ELA",
            "Term":36,
            "Quantity":1
        },
        {
            "ProductCode":"CON-ADD-IP4",
            "Term":36,
            "Quantity":0
        },
        {
            "ProductCode":"CON-ADD-INT",
            "Term":36,
            "Quantity":0
        }
    ]
}

this.getPrices1 = function () {
    return $http.post('http://www.website.com/getPriceNoAuth?json=' + productAttributes ).
        success(function(resp){
            //log something here.
        });
};

有人看到我在做什么錯嗎? 謝謝。

$http({
url:'http://myurl.com'
method:'POST',
data:{
  'json':productAttributes
}
});

如果確實需要從URL傳遞數據,則執行ORRR,將json字符串化並在服務器端將其解碼

$http.post('http://myurl.com?json=' + JSON.stringify(myJson));

您正在嘗試發布數據,但是您將其像get參數一樣放置在url中。 像這樣發布數據:

this.getPrices1 = function () {
    return $http.post('http://www.website.com/getPriceNoAuth', {json: productAttributes} ).
        success(function(resp){
            //log something here.
        });
};

http標頭未在其中定義..但默認情況下,它將json作為內容類型:

$ http.post('/ someUrl',data).success(successCallback);

在這里,您必須調用一個api,在其中我們必須使用get方法發送數據。 只需使用下面的代碼即可。 它對我有用,希望它也對您有用。

var dataObject = {
    json : productAttributes
};


var responsePromise = $http.get("http://www.website.com/getPriceNoAuth", dataObject, {});

responsePromise.success(function(dataFromServer, status, headers, config) {

    var outputDate=angular.fromJson(dataFromServer);

    if(outputDate.status==1)
    {
        angular.forEach(outputDate.storelist, function(value, key) {
            $scope.userstores.push({name:value.StoreName,id:value.StoreID});
        });
    }          
});

responsePromise.error(function(data, status, headers, config) {
    console.log("Error in fetching user store call!");
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM