Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery passing data to ajax function

I'm having problems passing data values to the Jquery Ajax function.

I have been using the getJSON function and that was working fine but now I want to use the ajax function and I can't work out how to pass in 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 this right? Can anyone tell me where i'm going wrong?

Thanks

like image 532
tmutton Avatar asked Feb 01 '11 17:02

tmutton


2 Answers

You have invalid JSON:

"{ 'id', '2' }"

I would recommend you calling it like this as it will take care of properly encoding 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');
    }
});
like image 87
Darin Dimitrov Avatar answered Oct 30 '22 14:10

Darin Dimitrov


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

like image 27
Arnaud Le Blanc Avatar answered Oct 30 '22 14:10

Arnaud Le Blanc