A-A+
jQuery中ajax post方法提交数据
ajax提交数据的方式有get,post了,在这里下面小编为各位整理了一个简单的ajax post方法提交数据例子,希望对各位有帮助。
例子:
- <!DOCTYPE html>
- <html>
- <head>
- <script src="js/jquery.min.js">
- </script>
- <script>
- $(document).ready(function(){
- $("button").click(function(){
- $.post("demo_test_post.asp",
- {
- name:"Donald Duck",
- city:"Duckburg"
- },
- function(data,status){
- alert("Data: " + data + "\nStatus: " + status);
- });
- });
- });
- </script>
- </head>
- <body>
- <button>发送POST数据并获取返回结果</button>
- </body>
- </html>
ajax post提交数据乱码
firefox下测试都正常,chrome和ie内核下都是有此问题,此问题设置了页面属性为utf-8时候,只有firefox是传的charset=utf-8的头文件chrome和ie都没有指定,所以出现乱码问题.
解决方法:
- $.ajaxSetup({
- contentType: "application/x-www-form-urlencoded; charset=utf-8"
- });
- $.post("test.php", { name: "i5a6", time: "2pm" },
- function(data){
- process(data);
- }, "json");
- //或者使用:
- $.ajax({
- url:url,
- type:"POST",
- data:data,
- contentType:"application/x-www-form-urlencoded; charset=utf-8",
- dataType:"json",
- success: function(){
- ...
- }
- })
总结一句:ajax乱码就是中文问题了,只要把页面编辑转成uft8 就可以了,通常可以使用contentType:来定义类型了,当然在接受时也可以使用转换函数来操作