Ajax入门(三)

时间:2023-03-10 06:40:10
Ajax入门(三)

get和post请求

1,get方式:

  • 在url地址后面以请求字符串(传递的get参数信息)形式传递数据.    例:  aj.open('get','./03.php?name=3tu');
  • 在传递特殊符号(中文,=,&)时,需要对其进行处理 
  • 在php中: 使用urlencode()/urldecode()方法对特殊字符进行编码和反编码
  • 在JavaScript中: 通过encodeURIComponent()方法对特殊符号等信息进行编码.  例:   name = encodeURIComponent(name);

2,post方式:

  • 以send(请求字符串数据)方法传递数据.  例:  var info = "name="+name;  ajx.send(info);
  • 如果发送单个信息,需要进行模拟post表单发送  ajx.setRequestHeader("content-type","application/x-www-form-urlencoded");
  • 同样需要对发送的特殊字符进行编码处理