jQuery Ajax成功函数数据是否为空?

时间:2014-01-30 12:06:10

标签: php ajax null

我是ajax/php的新手并且正在学习它。我试图通过ajax传递php值,这是成功的但是我无法从php文件获取响应变量ajax。虽然ajax成功,但为什么来自php的数据或结果或响应为NULL。这里有什么遗漏,我无法理解。请帮忙!

$(document).ready(function(){
$('#regform').submit(function(){
     var form = $(this),
     formData = form.serialize(),
     formUrl = form.attr('action'),
     formMethod = form.attr('method'), 

     $.ajax({
         url: formUrl,
         type: formMethod,
         data: formData,
         success:function(result){
                console.log(result)    =====> why its is NULL? 
                console.log(result.status);   ======>Uncaught TypeError: Cannot read 

       }
  });

});  })

HTML5文件:

<form id="regform" method="post" action="process.php">
<fieldset>
<label for="firstName">First Name<span style="color:red;">&#42;</span></label>
<input type="text" id="firstName" name="firstName" placeholder="First Name" required>
<label for="Company">Company</label>
<input type="text" id="Company" placeholder="" name="Company">
<label for="telephone">Phone Number</label>
<input type="tel" id="telephone" name="telephone">
<label for="email">Email<span style="color:red;">&#42;</span></label>
<input type="email" id="email" pattern="([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})" name="email" placeholder="example@domain.com" required>
<input name="submit" id="submit" tabindex="5" value="Register" type="submit"> 
</fieldset>
    </form>

和php文件: process.php

         $status="Success!!!";
         $message="You are welcome";

         $data = array(
            'status' => $status,
            'message' => $message
        );
        header('Content-Type: application/json');
        echo json_encode($data);
                    exit;

任何帮助,方向,对问题/解决方案的见解都会更受欢迎。先谢谢你。

1 个答案:

答案 0 :(得分:0)

添加以下代码,

$.ajax({
    type:"post",
    url: "process.php",
    data: 'name='+name,
    cache: false,
    dataType: 'json',
    success: function(result) {
        console.log(result) 
        console.log(result.status);    
    }
});

删除jQuery.parseJSON() ...使用json_encode()时无需解析...