1

I saw bunch of similar posts on stackoverflow, but neither worked for me. I have simple AJAX ENABLED WCF service, which returns cities based on input

 [OperationContract]
    public IEnumerable<string> GetCities(string prefix)
    {
        string[] cities = new[] { "New York", "Atlanta", "Los Angeles", "Las Vegas", "Arizona", "New Hampshire", "New England" };

        if(!String.IsNullOrEmpty(prefix))
        {
            cities = cities.Where(a => a.ToLower().StartsWith(prefix.ToLower())).ToArray();
        }

        return cities;
    }

Now I'm trying to make a call to this method using jquery ui's autocomplete. The problem is I have to pass prefix parameter to the method call, but whatever I'm trying the prefix parameter gets appended to url as a query string. Here's the jQuery code that I have

$("input[type=text][id*=autocompleteBox]").autocomplete({
    source: function (request, response) {
        var dataCity = {'prefix': request.term};
        $.ajax({
            url: "http://localhost:1939/Cities.svc/GetCities",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(dataCity),
            success: function (data) {
                response($.map(data, function (item) {
                    return {
                        label: data
                    }
                }));
            }
        });
    },
    minLength: 0
});

I know I have 3 options to call remote datasource. First option is the code above, but this way I have stringified json result appended to url.

Then I tried to simply pass url of service to source option, but I got the same result ( term=myinput appended to url ).

I can't use 3rd option ( an array with local data ) as I have many entries, I don't want to keep them all on the client.

So how can I pass prefix parameter to Service Method ? Is this possible, or I have to stuck with url appended parameters if I choose to use jquery ui's autocomplete widget ?

2 Answers 2

2

I don't see HTTP method specified in your ajax call (type parameter) so it defaults to GET and GET cannot have content. The only way to pass data when GET is used is through URL parameters. Btw. because you method just gets data, it should use GET request.

So instead of JSON, send just:

var dataCity = "prefix=" +  encodeURIComponent(request.term);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi thanks for quick response, I can't agree with the notion that I should use GET request to get data back. Actually I made some errors in the code, in particular I should map data.d as instead of data. I'll post complete code as an answer.
Sry forgot to mention, thanks for pointing out that I didn't specified type parameter.
1

Actually, I made some mistakes in the code. I should map returned result, which in this case is data.d. Here's the working code.

$("input[type=text][id*=autocompleteBox]").autocomplete({

        source: function (request, response) {

            var data = { 'prefix': request.term};

            $.ajax({
                type: "POST",
                url: "http://localhost:1939/Cities.svc/GetCities",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify(data),
                processdata: true,
                success: function (result) {
                    response($.map(result.d, function (item) {
                        return {
                            label: item
                        };
                    }));
                },
                error: function (er) {
                    alert(er);
                }
            });
        },
        minLength: 1,
        open: function () {
            $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },
        close: function () {
            $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.