无法使用AJAX发送POST请求

时间:2014-10-10 13:41:44

标签: php jquery ajax

我无法使用AJAX向我的php文件发送POST请求。我有两个文件,第一个是index.php,第二个是VerificationStatusAPIV2.php。 index.php的内容是:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Phone Verification by Dial2Verify API V2 ( www.dial2verify.com )</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
var attempt=1;
var transactionToken="";

    $(document).ready(function(){
        $("#enter_number").submit(function(e) {
         e.preventDefault();
         initiateDial2Verify();
        });
    });

    function initiateDial2Verify() {
        showCodeForm(1); 
        GetVerificationImage();
    }

    function showCodeForm(code) {
        $("#dial2verify").fadeIn();
        $("#enter_number").fadeOut();
        $("#waiting_msg").text("Waiting for missed call from "+$("#phone_number").val());
    }

    function GetVerificationImage() {
        $.post("GetImageAPIV2.php", { phone_number : $("#phone_number").val() }, 
               function(data) { updateImage(data.img,data.transactionToken); }, "json");
    }



    function updateImage(img, vtransactionToken) {




                             $("#Image").html("Please give a missed call to <br><img src=\""+img+"\"/>");

                         transactionToken = vtransactionToken;
                         PollStart("0");
                            }


    function CheckStatus()
    {
        $.post("VerificationStatusAPIV2.php", { transactionToken : transactionToken }, 
               function(data) { PollStart(data.status); }, "json");
    }


    function PollStart(vStatus)
    {
                     attempt =attempt+1;
                     if ( attempt >= 90  ) { TimeoutCheck(); }  
                     else
                     if (vStatus === "0") { 
                        $("#status").html("Please give a missed call in  <b><i>"+(90-attempt) +"</i></b> seconds.");
                        setTimeout(CheckStatus, 1000);
                        }                        
                     else if (vStatus === "1")
                    {
                    success(); 
                    }
                    else
                    TimeoutCheck();

    }


            function Err() {
            $("#status").html("Error!<br>Sorry something went wrong, Please cross check your telephone number."); 
            }

    function success() {
        $("#status").text("Congrats !!! Phone Number Verified!");

    }

    function TimeoutCheck() {
        $("#status").text("Verification Failed!");
    }

</script>
</head>
<body>
<form id="enter_number">
    <p>Enter your phone number:</p>
    <p><input type="text" name="phone_number" id="phone_number" /></p>
    <p><input type="submit" name="submit" value="Verify" /></p>
</form>

<div id="dial2verify" style="display: none;">
    <p id="waiting_msg"></p>
    <p id="Image">Loading ..</strong></p>
            <p id="status">Loading ..</strong></p>
</div>
</body>
</html>

在函数CheckStatus()中,我使用AJAX发送一个post请求来提交VerificationStatusAPIV2.php,但它没有发出任何帖子请求。谁可以告诉别人有什么不对?

更新我刚看到func CheckStatus()中“data.status”中的“status”为黄色,而其他颜色为蓝色。也许这就是问题?

2 个答案:

答案 0 :(得分:0)

在你的代码中剪切了{you}对函数it CheckStatus执行的唯一地方就是这行... setTimeout(CheckStatus, 1000);所以,如果这是函数调用,它应该是这样的: setTimeout(CheckStatus(), 1000);

这可能是你的问题。

答案 1 :(得分:0)

尝试改变 <input type="submit" name="submit" value="Verify" /><input type="submit" id="btn_enter" name="submit" value="Verify" />

并在js:

$("#btn_enter").submit(function(e) {
     e.preventDefault();
     initiateDial2Verify();
    });

您将提交功能指向HTML表单的ID

var request=$.ajax({
   method:"POST",
   url:"VerificationStatusAPIV2.php",
   data:{ transactionToken : transactionToken },
   dataType:"json"
  });
request.success(function(data){
  PollStart(data.status);
});