数据获取(数组以及接口)

This commit is contained in:
caicai8 2019-12-19 19:45:16 +08:00
parent d5658877e9
commit 923a260dbf
2 changed files with 81 additions and 67 deletions

View File

@ -23,3 +23,66 @@ $(function(){
function cancelVoice(){
$(".voicePanel").removeClass("active");
}
// ajax请求
function ajax(mJson)
{
var type=mJson.type||'get';
var url=mJson.url;
var data=mJson.data;
var success=mJson.success;
var error=mJson.error;
var dataStr='';
//console.log(data);
//请求数据封装
if(data)
{
var arr = Object.keys(data);//获取key数组以便获取长度
var len = arr.length;
var i = 0;
for (var key in data)
{
dataStr+=key+'='+data[key];
if (++i<len)
{
dataStr+='&';
}
}
if(type.toLowerCase()=='get')
{
url+='?'+dataStr;
}
// dataStr+=new Date().getTime();//设计时需要发送时间。。。
// if(type.toLowerCase()=='get')
// {
// url+='?'+dataStr;
// }
}
//console.log(url);
var xhr=new XMLHttpRequest();
xhr.open(type,url,true);
xhr.setRequestHeader('content-type' , 'application/x-www-form-urlencoded');
xhr.send(null);
xhr.onreadystatechange=function()
{
if(xhr.readyState==4)
{
if(xhr.status>=200&&xhr.status<300)
{
success&&success(xhr.responseText);
}
else
{
error&&error(xhr.status);
}
}
}
}

View File

@ -73,7 +73,6 @@
</div>
<script>
var params = window.location.href.split('?');
console.log(params);
$(function(){
if(params.length>1){
action('中印边境包含哪几段');
@ -95,9 +94,6 @@
},{
input:'印军近期在班公湖的活动?',
output:'印军在拉达克地区举行联合军演、印度北部军区司令到班公湖巡视。'
},{
input:'哟',
output:'耶~'
}
]
var text = $("#f-left");
@ -138,7 +134,23 @@
}
}
}
ajax(args);
var answer = "";
var flag = false;
for(var i = 0 ;i<array.length;i++){
if( array[i].input.indexOf(value) > -1 ){
flag = true;
answer = '<div class="rotWord"><span></span><p id="member">'+array[i].output+'</p></div>';
continue;
}
}
if(flag){
setTimeout(function(){
$(".b_body").append(answer);
$(".b_body").scrollTop(10000000);
},500);
}else{
ajax(args);
}
$(".b_body").scrollTop(10000000);
text.val('');
}
@ -154,68 +166,7 @@
}
});
function ajax(mJson)
{
var type=mJson.type||'get';
var url=mJson.url;
var data=mJson.data;
var success=mJson.success;
var error=mJson.error;
var dataStr='';
//console.log(data);
//请求数据封装
if(data)
{
var arr = Object.keys(data);//获取key数组以便获取长度
var len = arr.length;
var i = 0;
for (var key in data)
{
dataStr+=key+'='+data[key];
if (++i<len)
{
dataStr+='&';
}
}
if(type.toLowerCase()=='get')
{
url+='?'+dataStr;
}
// dataStr+=new Date().getTime();//设计时需要发送时间。。。
// if(type.toLowerCase()=='get')
// {
// url+='?'+dataStr;
// }
}
//console.log(url);
var xhr=new XMLHttpRequest();
xhr.open(type,url,true);
xhr.setRequestHeader('content-type' , 'application/x-www-form-urlencoded');
xhr.send(null);
xhr.onreadystatechange=function()
{
if(xhr.readyState==4)
{
if(xhr.status>=200&&xhr.status<300)
{
success&&success(xhr.responseText);
}
else
{
error&&error(xhr.status);
}
}
}
}
</script>
</body>
</html>