dqwh1208
2016-01-05 11:15
浏览 79

AJAX调用响应没有显示任何内容

I have this input field.

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

When submit button is clicked, it goes into :

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

    });
});

And the call is sent to:

$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!");
        }

Everything is working fine, But this call is not returning anything. I mean the console.log(e); prints nothing, what could be the reason?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • duanmao1975 2016-01-05 11:25
    已采纳

    plus other answers you must set json dataType in $.post function if you want to return values as json :

    $.post(ajax_url_store_email, {act:"Subscribe", value:value}, function(e){
      console.log(e);
    },'json');
    
    点赞 评论
  • duanguzhong5776 2016-01-05 11:18

    To return a response to the ajax request, you must echo not return:

    if($verify==true){
        echo json_encode( "Success!");
    }
    else{
        echo json_encode("Something went wrong!");
    }
    
    点赞 评论
  • douyu5775 2016-01-05 11:20

    try echo and exit instead of return

    $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;
    
    点赞 评论
  • douji5746 2016-01-05 11:23

    You must use PHP print or echo function instead of return.

    点赞 评论

相关推荐 更多相似问题