Solved

jquery autocomplete with ajax and special characters

Posted on 2013-01-02
1
702 Views
Last Modified: 2013-01-02
I'm trying to use the auto complete with a textbox. When I type characters like ', it gives an error about invalid object passed. How can I resolve this??

    <script type="text/javascript">
        $(document).ready(function () {
            $("#<%=txtQuestion.ClientID %>").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '<%=ResolveUrl("~/AutoComping.asmx/GetQuestions") %>',
                    data: "{ 'prefix': '" + request.term + "'}",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        response($.map(data.d, function (item) {
                            return {
                                label: item.split('-')[0],
                                val: item.split('-')[1]
                            }
                        }))
                    },
                    error: function (response) {
                        alert(response.responseText);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    }
                });
            },
            select: function (e, i) {
             

            },
           
        });
    });
</script>
0
Comment
Question by:NickMalloy
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 400 total points
ID: 38738322
replace : data: "{ 'prefix': '" + request.term + "'}",
by : data: "{ \"prefix\": \"" + request.term.replace(/"/g,"\\\"") + "\"}",
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now