js-ajax-json-unicode

本文探讨了AJAX访问同一URL时可能出现的数据错误问题,并提供了解决方案,即通过添加唯一标识来避免缓存问题。此外,还介绍了JSON数据处理及字符集转换的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ajax

ajax访问同一url时,第二次访问的结果会直接使用第一次访问的结果,导致数据错误,
为避免此情况的出现,在地址或参数后加唯一标识,new Date()之类的。
async: true,代表在执行ajax时页面其他动作也可以执行,为false时不执行完此ajax不可执行其他操作。
<script type="text/javascript">
            function access() {
                $.ajax({
                    type: "get",
                    dataType:"json",
                    url: "http://localhost:8080/Test/test/test",
                    async: true,
//                  statusCode: {
//                      404: function() {
//                          alert('page not found');
//                      }
//                  },
                    error: function(XMLHttpRequest) {
                        if(XMLHttpRequest.status=404){
                            console.log(XMLHttpRequest);
                        }
                    },
                    success: function(data,status,XMLHttpRequest) {
                        console.log("-----------------------------------");
                        console.log(data);
                        console.log("-----------------------------------");
                        console.log(status);
                        console.log("-----------------------------------");
                        console.log(XMLHttpRequest);

                    }
                });
            }
        </script>

json

 //JSON字符串转换为JSON对象
 var obj = eval('(' + str + ')');
 var obj = str.parseJSON(); 
 var obj = JSON.parse(str); 
 //取数据时使用.运算符,如:obj.msg
 //将JSON对象转化为JSON字符
 var str=obj.toJSONString();
 var str=JSON.stringify(obj);

unicode转utf-8

function UnicodeToUtf8(unicode) {
                var uchar;
                var utf8str = "";
                var i;
                for(i = 0; i < unicode.length; i += 2) {
                    uchar = (unicode[i] << 8) | unicode[i + 1]; //UNICODE为2字节编码,一次读入2个字节 
                    utf8str = utf8str + String.fromCharCode(uchar); //使用String.fromCharCode强制转换 
                }
                return utf8str;
            }

utf-8转unicode

function Utf8ToUnicode(strUtf8) { 
  var i,j; 
  var uCode; 
  var temp = new Array(); 
  for(i=0,j=0; i<strUtf8.length; i++){ 
    uCode = strUtf8.charCodeAt(i); 
    if(uCode<0x100){         //ASCII字符 
      temp[j++] = 0x00; 
      temp[j++] = uCode; 
    }else if(uCode<0x10000){ 
      temp[j++] = (uCode>>8)&0xff; 
      temp[j++] = uCode&0xff; 
    }else if(uCode<0x1000000){ 
      temp[j++] = (uCode>>16)&0xff; 
      temp[j++] = (uCode>>8)&0xff; 
      temp[j++] = uCode&0xff; 
    }else if(uCode<0x100000000){ 
      temp[j++] = (uCode>>24)&0xff; 
      temp[j++] = (uCode>>16)&0xff; 
      temp[j++] = (uCode>>8)&0xff; 
      temp[j++] = uCode&0xff; 
    }else{ 
      break; 
    } 
  } 
  temp.length = j; 
  return temp; 
}
function Utf8ToUnicode(unicode) {
    if (unicode >= 0x00000000 && unicode <= 0x0000007F) {
        return unicode;
    }
    else if (unicode >= 0x00000080 && unicode <= 0x000007FF) {
        var r1 = (((unicode & 0x7C0) >> 6) | 0xC0) << 8;
        var r2 = (unicode & 0x03F) | 0x80;
        return r1 | r2;
    }
    else if (unicode >= 0x00000800 && unicode <= 0x0000FFFF) {
        var r1 = (((unicode & 0xF000) >> 12) | 0xE0) << 16;
        var r2 = (((unicode & 0x0FC0) >> 6) | 0x80) << 8;
        var r3 = ((unicode & 0x003F) | 0x80);
        return r1 | r2 | r3;
    }
    else if (unicode >= 0x00010000 && unicode <= 0x0010FFFF) {
        var r1 = (((unicode & 0x1C0000) >> 18) | 0xE0) << 24;
        var r2 = (((unicode & 0x03F000) >> 12) | 0x80) << 16;
        var r3 = (((unicode & 0x000FC0) >> 6) | 0x80) << 8;
        var r4 = ((unicode & 0x00003F) | 0x80);
        return r1 | r2 | r3 | r4;
    }
    else {
        return false;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值