How to format data params when using GET with AJAX

How to format data params when using GET with AJAX

CDeCinkoCDeCinko Posts: 19Questions: 9Answers: 1

I really want to use GET to get the data for my grid but almost very example I find uses POST. Is POST better?

All I want to do is pass in a value from a dropdown that I use to filter the datatable. By default it filters by the first option in the dropdown list, and if I toggle to the second value I want to reload the data with the new filter param. I can get this to work with POST but with GET no matter what is selected in the dropdown, I always get back the default results.

    var table = $("#bannerAds").DataTable({
        ajax: {
            type: "GET",
            asynch: false,
                    cache: false,
            url: "/services/admin/BannerAds.asmx/getListofAds",
            contentType: "application/json; charset=utf-8",
            data: function(d) {
                return JSON.stringify("status: $('#status').val()");
            },
            dataType: "json",
            dataSrc: function (json) {
                return JSON.parse(json.d);
            },
            error: function () {
                alert("Unable to load data.");
            }
        },

Answers

  • CDeCinkoCDeCinko Posts: 19Questions: 9Answers: 1

    I ended up with this which puts encoded double quotes around the value.

                data: function(d) {
                    d.status = JSON.stringify($("#status").val());
                },
    

    Not sure if this is the proper way, but so far it seems to work.

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Is POST better?

    Not better, just different - it depends on your requirements.

    To use GET you would add query string parameter(s) to your url:

     url: "/services/admin/BannerAds.asmx/getListofAds?param_name=value",
    

    @Allan: is there any way to avoid having code obscured by the "Plain text" link?

This discussion has been closed.