jQuery 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;

对问题/解决方案的任何帮助,方向,见解将不胜感激。预先感谢您。

添加以下代码,

$.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() ...

时,无需解析

相关内容

  • 没有找到相关文章

最新更新