js如何获取response返回的数据

平常都是使用jq的ajax进行的前后台操作,如下。

   $.ajax({
         type: "GET",
         url: "/test.action",
         data: {username:$("#username").val(),content:$("#content").val()},
         dataType: "json",
         success: function(data){
                   //继续操作
                  }
     });

这真的是一个很方便的交互方式,今天突然想到一个问题,jquery是怎么获取到response的数据?或许说js是怎么获取response的数据的?

阅读 27k
3 个回答

success里的data拿到的不是response?

给你看一段我写的代码:https://github.com/bestime/ajax

    xhr.onreadystatechange = function() {
      if ( xhr.readyState == 4 ) {
        if ( xhr.status == 200 ) {
          var res = xhr.responseText 
          try { res = JSON.parse(res) } catch (e) {}
          success(res); // 成功回调
        } else {
          error(xhr); // 失败回调
        }
      }
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题