特殊字符断点json返回jQuery

编程入门 行业动态 更新时间:2024-10-09 10:30:19
本文介绍了特殊字符断点json返回jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下jQuery ajax函数运行PHP脚本,该脚本查询包含编码为UTF-8的条目的MySQL数据库:

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); } }); }

返回到函数的JSON对象包含经度和纬度

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.

每个名称加载正常,并且显示没有问题,除了一个地图标记对象。单个名称,其中包含字符ñ。此名称在JSON对象中作为null返回。

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?

推荐答案

我以前遇到这个问题,我发现有两个选项:

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

  • 在PHP中,使用PHP函数htmlentities将所有字符转换为HTML友好字符(php/manual/en/function.html .php),然后返回到jQuery函数。
  • 如果失败或字符未转换,您可以尝试使用base64编码和解码。在PHP中,您可以使用base64_encode函数(php/manual/en/function.base64-encode.php)对数据进行编码。然后在jQuery中,您可以使用许多base64编码和解码插件中的一个来解码数据。我在过去成功使用了此插件: plugins.jquery / project / base64-encode-and-decode
  • In PHP convert all characters into HTML friendly characters using the PHP function htmlentities (php/manual/en/function.htmlentities.php) before returning it to the jQuery function.
  • 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 (php/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: plugins.jquery/project/base64-encode-and-decode
  • 如果没有这些选项,可能值得看看字符集由您的网页使用。尝试并使用UTF-8编码你的HTML,看看是否有帮助。

    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.

    如果你使用了一个基本的PHP页面查询有问题的数据和没有使用jQuery和AJAX,打印出名称,但它仍然无法正确显示。如果不是,它也可能是MySQL的字符集问题。

    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.

    更多推荐

    特殊字符断点json返回jQuery

    本文发布于:2023-11-27 18:42:25,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1639157.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:断点   特殊字符   jQuery   json

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!