Angular,发送 json 到 API

Angular, sending json to an API

我正在尝试 post json 到 api 并且它给了我以下错误...

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

这是我要发送的 json 对象及其 http 请求...

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
}
});

ORRR 如果您确实需要从 url 传递数据,请将您的 json 字符串化并在服务器端解码

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

您正在尝试 post 数据,但您将其像获取参数一样放在 url 中。 Post 这样的数据:

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

http header 没有在那里定义..但默认情况下它认为 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!");
});