tags:

views:

30

answers:

1

I've got the following JQuery code, which works fine if the VendorDropDown.ClientID is an integer, or a string that can be converted to an integer, but breaks if I try to use a string value like "Microsoft". The PopulateSoftware function is an Asp.Net WebMethod that takes a string parameter named vendorId.

var pageUrl = '<%=ResolveUrl("~/Default.aspx")%>'
function PopulateSoftwareDropdown() {
    alert('{vendorId: ' + $('#<%=VendorDropDown.ClientID%>').val() + '}');
    $.ajax({    
        type: "POST",
        url: pageUrl + '/PopulateSoftware',
        data: '{vendorId: ' + $('#<%=VendorDropDown.ClientID%>').val() + '}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSoftwarePopulated,
        failure: function (response) {
            alert(response.d);
        }
    });
}
A: 

Use

 data: '{vendorId: \'' + $('#<%=VendorDropDown.ClientID%>').val() + '\'}'

this will effectively wrap the value with single quotes, which means that it is a string value..


more correctly it should be

data: {'vendorId': $('#<%=VendorDropDown.ClientID%>').val() }

This will pass as data the object we create (the {..} part), which will be converted by jquery to the correct format (string)

Gaby
It *should* be double quotes really, like this: `'{ "vendorId": "' + $('#<%=VendorDropDown.ClientID%>').val() + '"}'` in practicality, either will work, but better to be safe.
Nick Craver
@Nick, i think the whole data should not be a string at all, but the actual key and value should both be, indeed.. (*the value being automatically converted since the data passed is an actual object now..*)
Gaby