2015-02-06 55 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