Home Web Front-end JS Tutorial Ajax sending and receiving requests

Ajax sending and receiving requests

May 24, 2018 am 10:53 AM
ajax send ask

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

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<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('get','url');

   

  ajax.onreadystatechange = function(){

    if((ajax.readyState==4) && (ajax.status)==200){

      alert(ajax.responseText); // 返回的数据内容

    }else{

      alert('请求失败');

    }

  }

  // 发送请求,content是要发送的内容,如果没有则填null

   send(content);

  

   // 如果用的是post方式请求,要在send之前设置HTTP头

   ajax.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);

 

  

</script>

Copy after login

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to send files to others on TikTok? How to delete files sent to others? How to send files to others on TikTok? How to delete files sent to others? Mar 22, 2024 am 08:30 AM

How to send files to others on TikTok? How to delete files sent to others?

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

How to solve the 403 error encountered by jQuery AJAX request

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

How to solve jQuery AJAX request 403 error

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

How to get variables from PHP method using Ajax?

How to solve the problem of jQuery AJAX error 403? How to solve the problem of jQuery AJAX error 403? Feb 23, 2024 pm 04:27 PM

How to solve the problem of jQuery AJAX error 403?

How to use check-ins with home, different locations, and timers How to use check-ins with home, different locations, and timers Mar 24, 2024 am 09:31 AM

How to use check-ins with home, different locations, and timers

PHP vs. Ajax: Solutions for creating dynamically loaded content PHP vs. Ajax: Solutions for creating dynamically loaded content Jun 06, 2024 pm 01:12 PM

PHP vs. Ajax: Solutions for creating dynamically loaded content

Common application scenarios of the Head request method in Laravel Common application scenarios of the Head request method in Laravel Mar 06, 2024 pm 09:33 PM

Common application scenarios of the Head request method in Laravel

See all articles