欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
  由于浏览器的安全限制,大多数“Ajax”的要求,均采用同一起源的政策 ;即无法从不同的域,子域或协议中正确接收数据。
  
  如果一个jQuery.post()请求返回一个错误代码,它会静静的失败,除非脚本调用全局的.ajaxError()方法。在jQuery 1.5, 通过jQuery.post()返回的jqXHR对象的.error()方法也可用于错误处理。
  
  例子:
  
  Example: 请求 test.php 页面, 但是忽略返回结果
  
  $.post("test.php");
  
  Example: 请求 test.php 页面 并且发送url参数(虽然仍然忽视返回的结果)。
  
  $.post("test.php", { name: "John", time: "2pm" } );
  
  Example: 传递数组形式data参数给服务器 (虽然仍然忽视返回的结果)。
  
  $.post("test.php", { 'choices[]': ["Jon", "Susan"] });
  
  Example: 使用Ajax请求发送表单数据。
  
  $.post("test.php", $("#testform").serialize());
  
  Example: Alert 从 test.php请求的数据结果 (HTML 或者 XML,取决于返回的结果)。
  
  $.post("test.php", function(data) {
  
  alert("Data Loaded: " + data);
  
  });
  
  Example: Alert 从 test.cgi请求并且发送url参数的数据结果 (HTML 或者 XML,取决于返回的结果)。
  
  $.post("test.php", { name: "John", time: "2pm" },
  
  function(data) {
  
  alert("Data Loaded: " + data);
  
  });
  
  Example: 得到test.php的内容,存储在一个 XMLHttpResponse 对象中并且运用 process() JavaScript函数。
  
  $.post("test.php", { name: "John", time: "2pm" },
  
  function(data) {
  
  process(data);
  
  },
  
  "xml"
  
  );
  
  Example: Posts to the test.php page and gets contents which has been returned in json format (<?php echo json_encode(array("name"=>"John","time"=>"2pm")); ?>).
  
  $.post("test.php", { "func": "getNameAndTime" },
  
  function(data){
  
  console.log(data.name); // John
  
  console.log(data.time); //  2pm
  
  }, "json");
  
  Example: 用ajax传递一个表单并把结果在一个div中
  
  <!DOCTYPE html>
  
  <html>
  
  <head>
  
  <script src="https://code.jquery.com/jquery-latest.js"></script>
  
  </head>
  
  <body>
  
  <form action="/" id="searchForm">
  
  <input type="text" name="s" placeholder="Search..." />
  
  <input type="submit" value="Search" />
  
  </form>
  
  <!-- the result of the search will be rendered inside this div -->
  
  <div id="result"></div>
  
  <script>
  
  /* attach a submit handler to the form */
  
  $("#searchForm").submit(function(event) {
  
  /* stop form from submitting normally */
  
  event.preventDefault();
  
  /* get some values from elements on the page: */
  
  var $form = $( this ),
  
  term = $form.find( 'input[name="s"]' ).val(),
  
  url = $form.attr( 'action' );
  
  /* Send the data using post and put the results in a div */
  
  $.post( url, { s: term },
  
  function( data ) {
  
  var content = $( data ).find( '#content' );
  
  $( "#result" ).empty().append( content );
  
  }
  
  );
  
  });
  
  </script>
  
  </body>
  
  </html>

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h63989.shtml