Error inserting Apostrophe in component using Auto Complete

Asked

Viewed 728 times

4

I’m having trouble inserting the ' Apostrophe when I’m going to search for the component AutoComplete of Jquery UI.

Jquery

 $("#<%=txtHipotePai.ClientID%>").autocomplete({
     source: function(request, response) {

         $.ajax({
             url: '<%=ResolveUrl("../ws/AutoComplete.asmx/GetListaHipotese")%>',
             data: "{ 'prefixText': '" + 'pt-Br' +
                 "','idioma': '" + $("#<%=rblIdioma.ClientID%> :checked").val() + "'}",
             dataType: "json",
             type: "POST",
             contentType: "application/json; charset=utf-8",
             success: function(data) {
                 response($.map(data.d, function(item) {
                     return {
                         label: item.split('#')[1],
                         val: item.split('#')[0]
                     }
                 }))
             },
             error: function(response) {
                 alert(response.responseText);
             },
             failure: function(response) {
                 alert(response.responseText);
             }
         });
     },
     select: function(e, i) {
         $("#<%=hdfCodHipotesePai.ClientID%>").val(i.item.val);
     },
     minLength: 1
 });
  • Please edit your question by adding the Javascript code related to the field.

  • It is not recommended to put errors as external images. They are not always accessible to everyone.

1 answer

3


The problem is because you are trying to generate Json from String concatenations, and the moment you concatenate a String containing simple quotes the Json breaks.

For example:

{ 'prefixText': 'pt-Br','idioma': 'aqui tem ' aspas simples' }

Note that the above Json syntax is corrupted.

It would be more productive to create a real object, then serialize to Json.

Example:

...
data: JSON.stringify({
    'prefixText': 'pt-Br',
    'idioma': $("#<%=rblIdioma.ClientID%> :checked").val()
}),
...

See if it works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.