How to format data params when using GET with AJAX
How to format data params when using GET with AJAX
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
I ended up with this which puts encoded double quotes around the value.
Not sure if this is the proper way, but so far it seems to work.
Not better, just different - it depends on your requirements.
To use GET you would add query string parameter(s) to your url:
@Allan: is there any way to avoid having code obscured by the "Plain text" link?