S Topkaya
S Topkaya

Reputation: 51

How to use Select2 4.0.2 with JSON via Ajax request?

I try to do pagination in server side with web service (.asmx), problem is response. Response comes xml format not json.

<?xml version="1.0" encoding="utf-8"?><string xmlns="http://tempuri.org/">{"Total":5,"Results":[{"id":5,"text":"a a a"},{"id":4,"text":"name yenice"},{"id":2,"text":"user user1"},{"id":1,"text":"user user2"},{"id":3,"text":"user3"}]}</string>

Js code here :

  $("#test").select2({
            minimumInputLength: 2,
            minimumResultsForSearch: 10,
            ajax: {
                url: URL,
                dataType: "json",
                type: "GET",
                data: function (params) {

                    var c = {
                        searchTerm: '', // search term
                        pageNum: 1,
                        pageSize: 10
                    }
                    return c;
                },
                processResults: function (data) {
                    return {
                        results: $.map(data, function (item) {
                            return {
                                text: item.tag_value,
                                id: item.tag_id
                            }
                        })
                    };
                }
            }
        });

Upvotes: 1

Views: 523

Answers (1)

S Topkaya
S Topkaya

Reputation: 51

Finally i solve problem in server side :

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void Sample(string searchTerm, int pageSize, int pageNum)
    {
        //var result.... 
        Context.Response.Write(JsonConvert.SerializeObject(result)); 
    }

Upvotes: 1

Related Questions