AJAX的GET请求、POST请求

感谢:链接(视频讲解很详细)


AJAX(Asynchronous Javascript and XML):异步的JavaScript和XML(不需要刷新网页就可以更新网页数据

XML:百度百科 是一种数据格式


知识点补充

1、XML代码是什么:(可能你会发现和html很相似,它们的关系可以自行百度

<application android:label="@string/app_name" android:icon="@drawable/osg">
        <activity android:name=".osgViewer"
                  android:label="@string/app_name" android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application>

2、两种请求的区别:

     GET请求:不需要加密直接译成字符串传送

     POST请求:需要加密传送

3、每种请求均包含两部分:

     ①、发送请求

     ②、接受回传的数据 

GET请求注意和POST请求的区别

1、发送请求:

<script type="text/javascript">
        //下面的代码仅是发送请求
	var xhr=new XMLHttpRequest(); 
        //新建一个XMLHttpRequest对象
	xhr.open("GET","1.txt",true); //其中1.txt为本地文件,便于演示(可加url)
        //open中包含三个变量分别是:1、请求类型(GET/POST)2、url(包含请求数据) 3、是否异步(True/Fals)
	xhr.send();
        //发送请求
</script>

2、打开终端在network中可以看到请求的状态:

3、具体状态表示的含义如下图:(图源上文视频链接

4、上文仅为发送请求,获得具体回传信息代码如下:

<script type="text/javascript">
        //接收回传数据的代码
	xhr.onreadystatechange=function(){
		if(xhr.status==200&&xhr.readyState==4){ //满足正常状态(status==200&&readyState==4)
			console.log(xhr.response); 
		}
	}
</script>

5、1.txt中的文本如下:

6、调试器运行截图:

POST请求

发送请求和接受数据:

<script type="text/javascript">
    //发送请求
	var xhr=new XMLHttpRequest();
	xhr.open("POST","1.txt",'True');
        //url中不包含请求数据(区别GET)
	xhr.send('这是请求数据');
    //接受数据
	xhr.onreadystatechange=function(){
		if(xhr.status==200&&xhr.readyState==4){
			console.log(xhr.response);
		}
	} 
</script>
原文地址:https://www.cnblogs.com/ldu-xingjiahui/p/12594049.html