首页 » Web前端 » phpjs接口挪用技巧_Js调用api接口

phpjs接口挪用技巧_Js调用api接口

访客 2024-11-22 0

扫一扫用手机浏览

文章目录 [+]

function productClickHandler() { var xhr = new XMLHttpRequest(); // 创建xhr工具 xhr.onreadystatechange = function() { if(xhr.readyState == 4) { if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) { var result = JSON.parse(xhr.responseText); // 将字符串转化为工具,然后才能获取到返回字符串中的某一个值 console.log(result.totalCount); // 获取返回字符串中的某一个值 } else { alert('Request was unsuccessful: ' + xhr.status); } } } var url = 'http://study.163.com/webDev/couresByCategory.htm?' + "pageNo=1&psize=1&type=10"; // 获取课程列表,带参数的get要求 xhr.open('get', url, true); // 开启一个要求,但还没有向做事器端发起要求,实行后redayState的值变为1 xhr.send(null); // 向做事器端发起要求,实行后redayState的值变为2 // 补充:当做事器端开始返回要求数据的时候,浏览器端吸收到这个数据,redayState的值变为3。
// 当浏览器端结束要求时,redayState的值变为4,status的值变为200(表示要求成功),responseText变为相应的返回值。

}

然后可以通过getElementById("id")等方法获取到html元素,再利用.innerHTML将获取到的值插入html。

phpjs接口挪用技巧_Js调用api接口

phpjs接口挪用技巧_Js调用api接口
(图片来自网络侵删)
标签:

相关文章