如果数据是阿拉伯语,则在ajax函数中获取字母'a'

问题描述 投票:-1回答:3

我有一个api,它发送阿拉伯语字符串数据,需要使用jquery $ .ajax方法以html web形式显示,但不幸的是ajax只收到一个字符,即“a”响应,如下所示

{code: 200, status: "error", msg: "a", msg_en: "Invalid Username!!"}

但是当我在邮递员中执行api时它会告诉我这个

{"code":200,"status":"error","msg":"اسم المستخدم موجود بالفعل","msg_en":"Username already exists!!"}

这是我在check_user_name.php中的代码

<?php
require_once "../admin/utils/config.php";
require_once "../admin/utils/dbClass.php";
$objDB = new MySQLCN;
require_once "../admin/utils/functions.php";
$fun = new mFunctions;
require_once "lang.confg.php";

$response = array();

if( isset($_POST['user_name']) && $_POST['user_name'] != null){
    $user = $objDB->_get_user(null,$_POST['user_name'],null,null,null,null,array('visitor','lawyer','admin'));
    if( !empty($user) ){
        $response['code'] = 200; // successfull request
        $response['status'] = 'error';
        $response['msg'] = $_lang['user_name_exists'];
        $response['msg_en'] = 'Username already exists!!';
    }else{
        $response['code'] = 200; // successfull request
        $response['status'] = 'success';
        $response['msg'] = $_lang['user_name_available'];
        $response['msg_en'] = 'Username available!!';
    }
}else{
    $response['code'] = 200; // invalid paramters
    $response['status'] = 'error';
    $response['msg'] = $_lang['invalid_requests'];
    $response['msg_en'] = 'Invalid Username!!';
}
end:
echo json_encode($response);
exit(); 

这是ajax请求

$(document).on("change", 'input[name=user_name]', function(e) { 
        /* Act on the event */
        var user_name = $ (this).val();
        if(user_name.length >= 6 || !user_name.length <=1 ){
            $.ajax({
                type: 'POST',
                url: HOST_URL_API+'/check_user_name.php',
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data : { 'user_name':user_name }, // our data object
                success: function(data) {
                    console.log(data);
                    if (data.status == "error") {
                        $('input[name=user_name]').parent().addClass('has-error');
                        $('input[name=user_name]').parent().find('.help-block').html(data.msg);
                        $('input[name=user_name]').focus();
                        // alert(data.msg);
                    }else{
                        $('input[name=user_name]').parent().removeClass('has-error');
                        $('input[name=user_name]').parent().addClass('has-success');
                        $('input[name=user_name]').parent().find('.help-block').html('');
                    }
                },
                error: function(jqXHR, textStatus, errorThrown, data) {
                    alert(errorThrown);
                },
            });
            event.preventDefault();
        }else{
        // alert("Username must be at least 6 characters");
        }
    }); 

请好心,如果有人有解决方案,将会很有帮助,提前谢谢;)

php jquery ajax api arabic
3个回答
0
投票

尝试在php代码中添加以下行,这可能会在渲染unicode字符时解决问题。

header("Content-Type : application/json; charset=ISO-8859-1");

0
投票

请检查此解决方案希望这将解决您的问题我简单创建index.php并运行此包含header("Content-type: application/json; charset=ISO-8859-1");这解决问题。

if (isset($_GET['dataa'])) {
    $res['code'] =200;
    $res['status'] = 'error';
    $res['msg'] = (string) "اسم المستخدم موجود بالفعل";
    $res['msg_en'] = 'Username already exists!!';
    // header ("Content-Type: text/html; charset=utf-8");
    header("Content-type: application/json; charset=ISO-8859-1");
    echo json_encode($res);die;
}

在同一页面的html中获取测试响应的req并将文本附加到正文

<!DOCTYPE html>
  <html>
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title></title>
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

       </head>
        <body>
        <script type="text/javascript">
         $(document).ready(function(){
         $.ajax({
           url: "index.php",
           type: 'GET',
           data : {dataa : 'ss'},
            success: function(res) {
            console.log(res);
            $('body').append(res.msg);
        }
    });
  })
  </script>
</body>
</html>

欢呼好运cheers good luck


0
投票

问题解决了家伙,感谢您的努力,实际上是代码错误,还有另一个具有相同键名的数组正在替换数组键的值,仍然感谢您的所有努力。

© www.soinside.com 2019 - 2024. All rights reserved.