使用json的Ajax帖子不会发送数据或显示错误消息

时间:2018-04-14 04:27:12

标签: json ajax

我试图将联系表单数据发送到数据库并通过json获得响应。

我得到的唯一错误是:

  

SyntaxError:位于0的JSON中的意外标记T

我的阿贾克斯:

$(document).ready(function(){
  $('#send').on('click',function(e){
    e.preventDefault();

    var name = $('#name').val();
    var email = $('#email').val();
    var phone = $('#phone').val();
    var subject = $('#subject').val();
    var message = $('#message').val();

    $.ajax({
        url : '<?php echo $baseurl;?>/contactus.php',
        type: 'post',
        dataType : 'json',
        data : {name:name,email:email,phone:phone,subject:subject,message:message},
        success:function(response){ 
          if(response.type == 'error'){ 
              output = '<div class="error">'+response.text+'</div>';
          }else{
             $('#form')[0].reset();
              output = '<div class="success">'+response.text+'</div>';
          }
          $("#contact_results").html(output);
        },
        error: function(xhr, textStatus, error){
          console.log(xhr.statusText);
          console.log(textStatus);
          console.log(error);
        }
    });
  });
});

我的PHP:

header('Content-Type:application/json');

include('include-global.php');

if($_POST){

    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
        print "Can't access directly!";
        exit;
    } 

    $sender_name    = $_POST["name"];
    $sender_email   = $_POST["email"];
    $phone_number   = $_POST["phone"];
    $subject        = $_POST["subject"];
    $message        = $_POST["message"];

    if(strlen($sender_name)<3){
        $output = json_encode(array('type'=>'error', 'text' => 'First and lastname is too short!'));
        die($output);
    }
    if(strlen($subject)<3){ //check emtpy subject
        print json_encode(array('type'=>'error', 'text' => 'Subject too short'));
        exit;
    }
    if(strlen($message)<3){ //check emtpy message
        print json_encode(array('type'=>'error', 'text' => 'Message too short'));
        exit;
    }

    try{
        $database = new Connection();
        $db = $database->openConnection();
        $sql = "INSERT INTO contact SET name = :name, phone = :phone, subject = :subject, email = :email, message = :message";
        $qry = $db->prepare($sql);
        $qry -> bindParam(':name', $sender_name, PDO::PARAM_STR);
        $qry -> bindParam(':phone', $phone_number, PDO::PARAM_STR);
        $qry -> bindParam(':email', $sender_email, PDO::PARAM_STR);
        $qry -> bindParam(':subject', $subject, PDO::PARAM_STR);
        $qry -> bindParam(':message', $message, PDO::PARAM_STR);
        $qry -> execute();
    } catch (PDOException $e) {
        echo "There is some problem in connection: " . $e->getMessage();
    }

    if(!$qry){
        print json_encode(array('type'=>'error', 'text' => 'Error.'));
        exit;
    }else{
        print json_encode(array('type'=>'message', 'text' => 'Thank you '. $sender_name . '.'));
        exit;
    }
}

我不明白我做错了什么......

代码和JSON似乎是正确的,我从谷歌得到了例子。 我使用dataType,标题内容类型json但仍显示错误。

0 个答案:

没有答案