jQuery-ajax-.get,.post方法
在上一篇中详细介绍了jQuery中ajax局部方法$.load()的使用。下面来介绍两个全局方法$.get(),$.post()的使用。
1、这两个全局方法其实和上一篇中的$.load()方法使用是差不多的。只是多了第四个参数接收的数据类型。
<span ><script> $(function(){ /*//直接请求远程的php文件,是以get形式请求的 $("#button").click(function(){ $.get('remote.php',function(response, status, xhr){ //alert(response); //alert(status); //alert(xhr.statusText); $.each(xhr, function(k, v){ $("#box").append(k+'=>'+v+'<br/>'); }) }) })*/ /* //可以在请求的地址中传入参数,也可以在第三个参数上以字符串的形式传入参数 $("#button").click(function(){ $.get('remote.php?username=zhangsan','age=89',function(response, status, xhr){ alert(response); //alert(status); //alert(xhr.statusText); }) })*/ //可以在请求的地址中传入参数,也可以在第三个参数上以对象的形式传入参数 $("#button").click(function(){ $.get('remote.php',{ age:90, username:"zhangsan" },function(response, status, xhr){ alert(response); //alert(status); //alert(xhr.statusText); }) }) //如果需要返回的内容强制转换为我们所需的内容,可以使用第四个参数来接收返回来的内容。如果不传这个从参数,将会智能识别返回来的内容。最好是使用这个参数 /*$("#button").click(function(){ $.get('remote.php?username=zhangsan','age=89',function(response, status, xhr){ alert(response); //alert(status); //alert(xhr.statusText); },'json') })*/ }) </script></span>
上面的这些例子详细讲述了$.get()这个方法的使用,$.post()这个方法的使用方法也是和这个方法是一样的,只是http请求的方法不一样。还有传参数不能直接在URL后面直接加参数,但是也是可以在第二个参数中以字符串的形式或者是以对象的形式传参数。