簡體   English   中英

AJAX返回的字符串未驗證

[英]AJAX returned string not validating

我通過AJAX從PHP返回“ true” /“ false”(文本)到javascript中,
但是即使條件正確,我的JAVASCRIPT IF條件似乎也沒有執行。

Java腳本

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成功警告“ false”,

在此處輸入圖片說明

但是if條件永遠不會執行。 返回數據類型有問題嗎?
我在這里想念什么呢?

只是一個建議,因為當您發現問題時,我已經開始為您的問題研究此替代方法。

我不建議您為此類任務使用標題。 如您所見,它們對預先輸出很敏感。 您可以使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM