Manju DS
Manju DS

Reputation: 37

Unable to send json data using durandal JS using Knockout Binding

var   xyz=  { 
              a: ko.observable(),
              b: ko.observable(),
              c: ko.observable(),   
              d: ko.observable()

           };
        function setupControlEvents()
    {
        $("#save").on("click",handleSave);                    
    }

    function handleSave()
    {

        var data = ko.toJSON(xyz);
        //alert("data to send "+data);
        //var d = serializer.serialize(data);
        url ="../save";
    http.post(url,data).then(function(){
            alert("success");
            console("save was success");
         });

I am able to get the data but unable to save .. when i alert the data that i am sending i get this data to send {"a":"A","b":"B","c":"C","d":"D","observable":{"full":true}}

i tried to serialize with durandal's serialize.serialize() but still not working .. i think i am unable to send the data because i am getting obserbvable in json data so please kindly help me to solve this..

Upvotes: 3

Views: 1970

Answers (1)

margabit
margabit

Reputation: 2954

You are applying ko.toJSON() twice. The method post from the http plugin in Durandal is already doing it. Take a look at the method code:

post:function(url, data) {
        return $.ajax({
            url: url,
            data: ko.toJSON(data),
            type: 'POST',
            contentType: 'application/json',
            dataType: 'json'
        });
    }

So you only need to do this:

url ="../save";
http.post(url,xyz).then(function(){
        alert("success");
        console("save was success");
});

Upvotes: 2

Related Questions