2016-01-05 58 views
0

我有這個輸入字段。AJAX呼叫響應沒有顯示任何內容

<input type="email" name="email" required/> 

當點擊提交按鈕,它進入:

$(".store_email").click(function() 
{ 
    var ajax_url_store_email = "store_email.php"; 
    var value= document.getElementsByName('email')[0].value; 


    $.post(ajax_url_store_email, {act:"Subscribe", value:value}, function(e){ 
     console.log(e); 

    }); 
}); 

,呼叫被髮送到:

$email=$_POST["value"]; 
$verify= $link->query(" INSERT INTO `Subscribers_email`(`email`) VALUES ('$email') "); 
     if($verify==true) 
     { 
      return json_encode("Success!"); 
     } 
     else 
     { 
     return json_encode("Something went wrong!"); 
     } 

一切工作正常,但此調用不返回任何東西。我的意思是console.log(e);什麼都不打印,可能是什麼原因?

+0

請檢查您的AJAX –

+0

嘗試回聲和退出,而不是在PHP代碼的回報 – Chintan

回答

1

加上其他的答案,如果你想返回值JSON必須設置在$。員額功能JSON數據類型:

$.post(ajax_url_store_email, {act:"Subscribe", value:value}, function(e){ 
    console.log(e); 
},'json'); 
1

要返回給Ajax請求的響應,則必須echo沒有return

if($verify==true){ 
    echo json_encode("Success!"); 
} 
else{ 
    echo json_encode("Something went wrong!"); 
} 
1

嘗試呼應和退出,而不是回報

$verify= $link->query(" INSERT INTO `Subscribers_email`(`email`) VALUES ('$email') "); 
     if($verify==true) 
     { 
      echo json_encode("Success!"); 
     } 
     else 
     { 
     echo json_encode("Something went wrong!"); 
     } 
     exit; 
1

您必須使用PHP printecho函數而不是返回。