2015-02-06 52 views
0

我試圖張貼JSON一個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. 
     }); 
}; 

有沒有人看到我做錯了什麼?謝謝。

回答

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

ORRR如果你真的需要從URL傳遞數據的字符串化JSON和它在服務器端進行解碼

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

@LeeWillis我不知道用戶的需求,也許他需要它是你的透視嗎? :) – sbaaaang 2015-02-06 14:25:17

+0

它只是一個內部應用程序的少量數據。你是爲我工作的。謝謝 – 2015-02-06 14:38:12

+0

@Josethehose不客氣! – sbaaaang 2015-02-06 14:39:36

0

你試圖發佈數據,但是你把它在url中像一個get參數。交的數據是這樣的:

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

在HTTP報頭中有沒有定義..但通過默認它認爲JSON作爲內容類型:

$ http.post( '/ someUrl',數據)。成功(successCallback);

0

這裏你必須調用一個api,其中我們必須使用獲得的 方法發送數據。只需使用下面的代碼即可。它適用於我,希望它 也將爲你工作。

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!"); 
}); 
+0

感謝您的回覆 – 2015-02-06 14:47:22