Heim  >  Artikel  >  Web-Frontend  >  Jquery中ajax方法data参数的用法小结_jquery

Jquery中ajax方法data参数的用法小结_jquery

WBOY
WBOYOriginal
2016-05-16 17:00:431090Durchsuche
复制代码 代码如下:

$.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston", //第一种方式传参
  // data: {name:"John",location:"Boston"}  //第二种方式传参
  // data: {foo:["bar1", "bar2"]} 转换为 '&foo=bar1&foo=bar2'
  /*
 第一种我们用url传参,参数里面如果加带"&"这个符号的话,可能参数接收不到或不完整,
 如“ data: "name=John&location=Boston",” 如果name的值是"john&smith"这样写可能就会有问题,
 我们可以用JS里面的encodeURIComponent()方法进行转义,
 但如果用data: {name:"John",location:"Boston"}这种方式写的话就不需要进行转义,
 如果转义的话,接收的将是转义后的字符串
  */
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
});
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn