AJAX返回的字符串没有验证

时间:2017-11-09 22:14:56

标签: javascript php ajax

我回来"真" /"假" (文本)从PHP到javascript通过AJAX,但我的 JAVASCRIPT IF条件似乎没有执行,即使条件是正确的。

的Javascript

var request = $.ajax({
type: "POST",
dataType: "text",
url: "",
data: { 
    'clientSecret': clientSecret,
    'oauthCode':oauthCode   
},
});

request.done(function(msg) {

    alert(msg);
    if(msg=="false"){       <----- DOES NOT EXECUTE
          //do stuff
    }



});

request.fail(function(jqXHR, textStatus) {
  alert( "Request failed: " + textStatus );
});

PHP

if(checkData($key,$oauthCode)==true){
        header("Content-Type: text/plain");
        echo "true"; 
        }
else{
        header("Content-Type: text/plain");
        echo "false";
        }

Javascript成功提醒&#34; false&#34;,

enter image description here

但if condtion永远不会被执行。 返回数据类型有问题还是?我在这里失踪了什么?

1 个答案:

答案 0 :(得分:0)

只是一个建议,因为当你发现问题时我已经开始研究这个问题的替代方案。

我不建议您使用标题来执行此类任务。正如你所看到的,它们对预先输出是明智的。您可以使用json方法(dataType: 'json') - 正如@charlietfl建议的那样,或使用dataType = 'html'

json看起来像这样:

的index.php

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
        <meta charset="UTF-8" />
        <!-- The above 3 meta tags must come first in the head -->

        <title>Test</title>

        <!-- jQuery -->
        <script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

        <script type="text/javascript">
            function startTestScript() {
                var clientSecret = $('#clientSecret').val();
                var oauthCode = $('#oauthCode').val();

                var ajax = $.ajax({
                    method: 'post',
                    dataType: 'json',
                    url: 'checkOauth.php',
                    data: {
                        'clientSecret': clientSecret,
                        'oauthCode': oauthCode
                    }
                });
                ajax.done(function (response, textStatus, jqXHR) {
                    if (response === true) {
                        alert('Response is ' + response + '. COOL!');
                    } else {
                        alert('Response is ' + response + '. UPS!');
                    }
                });
                ajax.fail(function (jqXHR, textStatus, errorThrown) {
                    alert('Request failed: ' + textStatus + '\n' + errorThrown);
                });
                ajax.always(function (response, textStatus, jqXHR) {
                    // alert('Finished executing this cool script.');
                });
            }
        </script>

        <style type="text/css">
            body {
                padding-top: 30px;
                text-align: center;
            }
            button {
                padding: 10px;
                background-color: #8daf15;
                color: #fff;
                border: none;
            }
        </style>
    </head>
    <body>

        <div>
            <label for="clientSecret">Client secret (int)</label>
            <input type="text" id="clientSecret" name="clientSecret" value="123">

            <br/><br/>

            <label for="oauthCode">OAuth code (int)</label>
            <input type="text" id="oauthCode" name="oauthCode" value="123">

            <br/><br/>

            <button type="button" name="testButton" onclick="startTestScript();">
                Start ajax
            </button>
        </div>

    </body>
</html>

checkOauth.php

<?php

/**
 * Check data.
 * 
 * @param mixed $value1
 * @param mixed $value2
 * @return TRUE if values are equal, FALSE otherwise.
 */
function checkData($value1, $value2) {
    return intval($value1) === intval($value2);
}

// Fetch posted values.
$clientSecret = isset($_POST['clientSecret']) ? $_POST['clientSecret'] : 0;
$oauthCode = isset($_POST['oauthCode']) ? $_POST['oauthCode'] : 0;

// Check data.
$dataIsValid = checkData($clientSecret, $oauthCode);

// Output the JSON-encoded result of checking data.
echo json_encode($dataIsValid);