JQuery passes data to ajax function

I am having problems passing data to a Jquery Ajax function.

I am using the getJSON function and this works fine, but now I want to use the ajax function and I cannot figure out how to pass the values.

       $.ajax({
            type: "POST",
            url: '../../../WebServices/ImageLibrary.svc/getimagesinfolder',
            dataType: 'json',
            data: "{ 'id', '2' }",
            contentType: "application/json; charset=utf-8",
            success: function (data)
            {
                alert('hello');
            }
        });

Is it correct? Can someone tell me where I'm going wrong?

thank

+5
source share
2 answers

You have invalid JSON:

"{ 'id', '2' }"

I would recommend you call it that because it will take care of the correct encoding of your parameters:

$.ajax({
    type: "POST",
    url: '../../../WebServices/ImageLibrary.svc/getimagesinfolder',
    dataType: 'json',
    data: JSON.stringify({ id: '2' }),
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        alert('hello');
    }
});
+9
source

Use this: data: { 'id': '2' },

+3
source

All Articles