How to pass a value with dataSource.read()?

2 Answers 284 Views
Data Source
Tayger
Top achievements
Rank 1
Iron
Tayger asked on 20 Dec 2021, 12:01 PM | edited on 20 Dec 2021, 12:08 PM

Hi

I would like to send a value (userid) with the read command of dataSource. I've done several tests but I can't catch the posted value in PHP.

Working$userid = $_POST["userid"] = 1234 on server side):

            $.ajax({
                type: 'POST',
                url: 'readparameter.php',
                data: {userid: "1234"},
                async: true,
                dataType: 'json',
                success: function(result) {      
                   console.log (result);
                }
            });


Not working ( $userid $_POST["userid"] = NULL on server side):

var mySource = new kendo.data.DataSource({
            dataType: 'json',
            transport: {
                read: function (options) {
                    $.ajax({
                        type: "POST",
                        dataType: 'json',
                        async: true,
                        data: {userid: "1234"},
                        url: "readparameter.php",
                        success: function (result) {
                            console.log (result);
                        }
                    });
                }
            }
        });

My assumption is that the posted userid never reaches the server using dataSource. I'm trying since a lot of time and tried several ways like also Json.stringify etc. What am I doing wrong or is it just not possible to pass a value with dataSource?

2 Answers, 1 is accepted

Sort by
0
Accepted
Tayger
Top achievements
Rank 1
Iron
answered on 23 Jan 2022, 08:04 PM | edited on 23 Jan 2022, 08:05 PM

Ok, the solution is to add the "type" parameter. Example:


var mySource = new kendo.data.DataSource({

    transport: {
        read: {
            url: "../php/readpage.php",
            dataType: "json", 
            type: "POST",
            data:  {
                skip: 0,
                take: 2
            }
        }
    }
});


0
Ianko
Telerik team
answered on 22 Dec 2021, 02:05 PM

Hi Tayger,

There is a dedicated property option you can use for each of the CRUD operations called data: https://docs.telerik.com/kendo-ui/api/javascript/data/datasource/configuration/transport.read#transportreaddata. I am mystified why the ajax call that is wrapped with the read function could suffer from that brahavior. However, the proper and supported way is to use the DataSource.Transport's data property instead.

Regards,
Ianko
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tayger
Top achievements
Rank 1
Iron
commented on 22 Dec 2021, 05:13 PM

Thanks for the answer. Of course I've tried that as well (datasource of a KendoUiDropdownlist). Didn't work either. As workaround I have created a pure $.ajax call and fill up the dropdownlist manually, works.
Ianko
Telerik team
commented on 23 Dec 2021, 06:34 AM

I suggest you checking the request headers of the sent AJAX request and compare it with the successful one. Let me know what are the differences so that the data i snot passed through the data option.
Tags
Data Source
Asked by
Tayger
Top achievements
Rank 1
Iron
Answers by
Tayger
Top achievements
Rank 1
Iron
Ianko
Telerik team
Share this question
or