Home  >  Article  >  Web Front-end  >  Ajax sending and receiving requests

Ajax sending and receiving requests

亚连
亚连Original
2018-05-24 10:53:522056browse

这篇文章主要为大家详细介绍了Ajax发送和接收请求的相关资料,感兴趣的小伙伴们可以参考一下

首先Ajax的不刷新页面提交数据,现在应用非常广泛,废话不多说马上进主题!!

基本上浏览器能接收的信息,Ajax都可以接收,ex:字符串,html标签,css标签,xml格式内容,json格式内容等等.....

<script>
  // IE浏览器
  if(ActiveXObject){
     // 微软目前AJAX最新版本
    var ajax = new ActiveXObject("Msxm12.XMLHTTP.6.0"); 
  }else{
    // 主流浏览器
    var ajax = new XMLHttpRequest();
   }
  
  // 创建HTTP请求
   // open(method, url, asynchronous, user, password);
   // method:请求方法(post,get)
   // url:请求地址(是具体要接收数据的地址)
   // asynchronous:同步或异步请求(true是异步,false是同步,默认是true,可不填)
   // user:(指定请求用户名,可不填)
   // password:(指定请求密码,可不填)
  
  ajax.open(&#39;get&#39;,&#39;url&#39;); 
  
  ajax.onreadystatechange = function(){
    if((ajax.readyState==4) && (ajax.status)==200){
      alert(ajax.responseText); // 返回的数据内容
    }else{
      alert(&#39;请求失败&#39;);
    }
  }
  // 发送请求,content是要发送的内容,如果没有则填null
   send(content);
   
   // 如果用的是post方式请求,要在send之前设置HTTP头
   ajax.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);

  
</script>

ajax的onreadystatechange事件最多接收四个变化状态 

readystate的返回状态值:

  0 (未初始化) 对象已建立

  1(初始化) 已调用open方法

  2(发送数据) 已调用send方法

  3(数据传送中) 已返回部分数据

  4 (完成) 请求成功

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

Ajax解决缓存的5种方法总结

解决AJAX请求中含有数组的办法

 Ajax请求和Filter配合案例解析

The above is the detailed content of Ajax sending and receiving requests. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn