1

i am trying to do ajax call by passing serialized json object and assigning it to data property of the ajax call. but some thing is not proper at assigning serialized object to data property control goes to jquery-2.0.3.min.js file

<script type="text/javascript">
    function AddEmployee() 
    {


        var Product = new Object();
        Product.Name = "kRISH";
        Product.Price = "23";
        Product.Category = "AS";

        $.ajax
        ({
            url: 'http://localhost:62310/api/products',
            type: 'POST',
            data: JSON.stringify(Product),
            contentType: "application/json;charset=utf-8",
            success: function (data){ WriteResponse(data);},
            error: function (x, y, z){ alert(x + '\n' + y + '\n' + z);}
        });
    }
</script>
3
  • data: JSON.stringify(Product : Product) ? Commented Dec 16, 2013 at 10:43
  • There is something wrong with your syntax. JSON.stringify(Product : Product). It should be something like JSON.stringify(products) where products is a JSON object. Commented Dec 16, 2013 at 10:44
  • can you specify tutorial that can gain me knowledge on web api , jquery , json Commented Dec 16, 2013 at 11:30

3 Answers 3

1

Either you want to serialize an object with a property Product, then you need

data: JSON.stringify( {Product : Product} )

or you just want to serialize your Product, then you need

data: JSON.stringify(Product)

btw your object initialization could be rewritten as:

var Product = {
  Name : "kRISH",
  Price : "23",
  Category : "AS"
};
Sign up to request clarification or add additional context in comments.

3 Comments

please re-check i had made a mistake while posting the question.JSON.stringify(product) also does not executes.it goes to jquery-2.0.3.min.js file
can you specify tutorial that can gain me knowledge on web api , jquery , json
@SunilJadhav There should be plenty on the net and you should be able to find them using google. I would recommend to start with the respective jQuery Docu or in general MDN.
1

Product : Product is not an object.

You should replace it with an actual object :

$.ajax
    ({
        url: 'http://localhost:62310/api/products',
        type: 'POST',
        data: JSON.stringify({Product : Product}),
        contentType: "application/json;charset=utf-8",
        success: function (data){ WriteResponse(data);},
        error: function (x, y, z){ alert(x + '\n' + y + '\n' + z);}
    });

http://jsfiddle.net/floo51/x52GX/

2 Comments

PLEASE RE CHECK i had made a mistake while posting the question.\
please re-check i had made a mistake while posting the question.
0

Just do

var json_text = JSON.stringify(Product, null, 2);

 $.ajax
        ({
            url: 'http://localhost:62310/api/products',
            type: 'POST',
            data: json_text ,
            contentType: "application/json;charset=utf-8",
            success: function (data){ WriteResponse(data);},
            error: function (x, y, z){ alert(x + '\n' + y + '\n' + z);}
        });

6 Comments

Why use pretty printing, when only transmitting to a server?
can you specify tutorial that can gain me knowledge on web api , jquery , json
@SunilJadhav : developer.mozilla.org is your friend For JSON , it is developer.mozilla.org/en/docs/JSON For Jquery , JQuery documentation is good
Now json string is serialized but when control comes to $.ajax goes to java script file and some internal lines are executed.
Eh , I donot understand
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.