ITPub博客

首页 > IT职业 > IT生活 > js-Ajax-get和post请求

js-Ajax-get和post请求

IT生活 作者:bluetooth 时间:2021-09-09 10:07:58 0 删除 编辑

1:get请求方式:

// 1:创建XMLHttpRequest对象var xhr;if (window.XMLHttpRequest) { // 其他类型的浏览器    xhr = new XMLHttpRequest();} else { // ie浏览器    xhr = new ActiveXObject("Microsoft.XMLHTTP");}// 2:配置请求信息xhr.open('get', 'ProcessShow.ashx', true);// 3:发送请求xhr.send();// 4:监听状态 注册onreadystatechange事件xhr.onreadystatechange = function() {    // 5:判断请求和相应是否成功    if (xhr.readyState == 4 && xhr.status == 200) {        // 6:获取数据 并做相应的处理        var data = xhr.responseText;     }}


这是一个完整的Ajax的get请求步骤。

如果get请求需要传递数据,就这样写:

xhr.open('get', 'ProcessShow.ashx?id=1&name=rose', true);

2:post请求方式:

// 1:创建XMLHttpRequest对象var xhr;if (window.XMLHttpRequest) { // 其他类型的浏览器    xhr = new XMLHttpRequest();} else { // ie浏览器    xhr = new ActiveXObject("Microsoft.XMLHTTP");}// 2:配置请求信息xhr.open('post', 'ProcessShow.ashx', true);// 3:发送请求xhr.send();  // 为空表示没有任何的参数// 4:监听状态 注册onreadystatechange事件xhr.onreadystatechange = function() {    // 5:判断请求和相应是否成功    if (xhr.readyState == 4 && xhr.status == 200) {        // 6:获取数据 并做相应的处理        var data = xhr.responseText;     }}


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3402/viewspace-2817076/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: golang中使用redis
请登录后发表评论 登录
全部评论

注册时间:2019-07-11

  • 博文量
    280
  • 访问量
    20976