【问题标题】:Angular, sending json to an APIAngular,将 json 发送到 API
【发布时间】:2015-02-06 13:53:39
【问题描述】:

我正在尝试将 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.
        });
};

有人看到我做错了吗?谢谢。

【问题讨论】:

    标签: javascript json angularjs rest


    【解决方案1】:
    $http({
    url:'http://myurl.com'
    method:'POST',
    data:{
      'json':productAttributes
    }
    });
    

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

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

    【讨论】:

    • @LeeWillis 我不知道用户需要,也许他需要它,你是千里眼吗? :)
    • 它只是内部应用程序的少量数据。你的独奏为我工作。谢谢
    【解决方案2】:

    您正在尝试发布数据,但您将其像 get 参数一样放在 url 中。像这样发布数据:

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

    【讨论】:

      【解决方案3】:

      http 标头没有在其中定义.. 但默认情况下它认为 json 作为内容类型:

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

      【讨论】:

        【解决方案4】:

        这里你必须调用一个 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!");
        });
        

        【讨论】:

        • 感谢您的回复
        猜你喜欢
        • 1970-01-01
        • 2017-03-09
        • 2012-12-01
        • 2017-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-12
        相关资源
        最近更新 更多