74002

Special characters break json returned to jQuery

The following jQuery ajax function runs a PHP script that queries a MySQL database containing entries that are encoded as UTF-8:

function searchLocations() { var stateSelected = $("#stateSelect").val(); $.ajax({ url: 'ajax/json.php', dataType: 'json', data: 'state='+stateSelected, success: function(data) { placeMarkers(data.markerdata.markers); } }); }

The JSON object returned to the function contains the longitudes and latitudes of map marker objects as well as a name to display in an info window when each marker is clicked.

Every name loads fine, and is displayed without a problem except for a single name which contains the character "ñ". This name is returned in the JSON object as "null". How can I make this name display properly?

Answer1:

I used to have this problem and I found there was two options:

<ol> <li>In PHP convert all characters into HTML friendly characters using the PHP function htmlentities (http://php.net/manual/en/function.htmlentities.php) before returning it to the jQuery function.</li> <li>If that fails or the characters are not converted you could try using base64 encode and decode. In PHP you can encode the data using base64_encode function (http://php.net/manual/en/function.base64-encode.php). Then in jQuery you could use one of many base64 encode and decode plugins to decode the data. I have used this plugin successfully in the past: http://plugins.jquery.com/project/base64-encode-and-decode</li> </ol>

Failing both those options it may be worth looking at the character set used by your page. Try and use UTF-8 to encode your HTML and see if that helps.

Without using jQuery and AJAX if you did a basic PHP page that queried the data in question and prints the name out does it still not display correctly. If it doesn't it could also be a character set issue with MySQL.

Hope any of these pointers help. Let me know how you get on.

Recommend

  • Google Maps Coordinates Incorrect [duplicate]
  • Shortest path using php [closed]
  • Reading off annotations of MKMapViews in NSArray
  • MySQL Function to calculate distance between two latitudes and longitudes [closed]
  • Email SAS html output
  • C#: Decoding JPEG images with 12-bit precision using Silverlight FJCore library?
  • iterparse fails to parse a field, while other similar ones are fine
  • Attemping to change attributes of $(this) inside success function
  • Remove previous Directions Route Google Maps
  • How to add regEx in angular filter
  • insert a picture into database(sqlite) with java code. what should i do?
  • How to synchronize two scrollview in Android?
  • Using android opencv apps without downloading opencv sdk manager
  • Google OAuth: can't get refresh token with authorization code
  • Add reference to ASP.NET 5 Class Library from Framework 4.5 Class Library Project
  • How Get arguments value using inline assembly in C without Glibc?
  • How to make R's read_csv2() recognise the text characters properly
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Implementation of State Monad
  • Caching attributes in superclass
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Play WS (2.2.1): post/put large request
  • Is it possible to access block's scope in method?
  • Get data from AJAX - How to
  • SignalR .NET Client Invoke throws an exception
  • Different response to non-authenticated users and AJAX calls
  • Bug in WPF DataGrid
  • Akka Routing: Reply's send to router ends up as dead letters
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • AT Commands to Send SMS not working in Windows 8.1
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • WPF Applying a trigger on binding failure
  • How do I configure my settings file to work with unit tests?
  • embed rChart in Markdown
  • Is it possible to post an object from jquery to bottle.py?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app