Home > Backend Development > PHP Tutorial > 请教ajax post表单,php如何接收

请教ajax post表单,php如何接收

WBOY
Release: 2016-06-13 11:53:52
Original
1149 people have browsed it

请问ajax post表单,php怎么接收?
a.html
var params=$("#form1").serialize();
$.ajax({
    type: "post",
contentType: "application/text",
data:params,
dataType:"json",
//dataType: "binary",
        async: false,
timeout: 30000,
        url: '1.php',
        success: function(data){
                //处理data数据
var s = eval(data);
     alert(s.a+'  '+s.b);
        },
error: function(x, e) {
                   // alert(x.readyState);
alert(e);
                }
});
-------------------------------
1.php
$db = new mysqli('localhost','adb','winst','test');
mysqli_query($db, "set character set 'utf8'");
$arr = $_REQUEST;  
$vnameid =$arr['username'];
$vtele =$arr['telephone'];
$query = "INSERT INTO cardvip(namevip,tel) VALUES ('"+$vnameid+"','"+$vtele+"')";
$db->query($query);

$data = json_encode(array('a'=>$vnameid, 'b'=>$vtele)); 
echo $data;
?>

如果用$vnameid ='username';
$vtele ='telephone';
1.html alert(s.a+'  '+s.b);可正确显示。

就是接收不到post数据
------解决方案--------------------
注释掉 dataType:"json"
alert(s.a+'  '+s.b); 改成 alert(data);

1.php
print_r($_POST);
exit;

用此改动进行调试

------解决方案--------------------
你的ajax参数dataType:"json",传递的却是serialize序列化的数据。
所以应该去掉dataType:"json"。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template