0

Jqueryを使用してPOSTを実行し、ページを認証しています。

$.post("api/authenticate", {"authkey": authkey}, function(data){
    console.log(data);
    if (data.success === "false") {
    window.location="/Login.html";
    } 
});

編集!!

認証が失敗した場合、私のphp関数はjsonオブジェクトを返します

{"success":"false"}

ただし、console.log(data)は何も返しません。リソースで応答を確認できますが。

どうすればこれを解決できるか知っている人はいますか?

どんな助けでも大歓迎です。

4

3 に答える 3

1

JSON が必要な場合は、$.getJSON を使用します。応答を JSON オブジェクトとして解析します。

$.getJSON("api/authenticate", {"authkey": authkey}, function(jsonObj){
    console.log(jsonObj);
    if (jsonObj.success === "false") {
       window.location="/Login.html";
    } 
});

http://api.jquery.com/jQuery.getJSON/

于 2012-10-25T16:39:38.870 に答える
1

Freonの答えはおそらくうまくdataTypeいきます.JSONへの強制を試してみてください.

// This is the signature for $.post
jQuery.post( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] )

$.post("api/authenticate", {"authkey": authkey}, function(data){
    console.log(data);
    if (data.success === "false") {
        window.location="/Login.html";
    } 
}, "json");

探すべきもう 1 つの考えは、サーバーが 2xx ステータスで応答することを確認することです。別のステータスを返した場合jQuery、応答を読み取ろうとしません。代わりに、 http://api.jquery.com/jQuery.ajax/statusCodeに渡されるオプションで設定されたステータス コード ハンドラーを探します。

jQuery.ajax( "api/authenticate", {
    data: {"authkey": authkey},
    dataType: 'json',
    statusCode: {
       306: function(jqXhr, errorType) {
            alert('Could not login')
            // If you still want to access the response, it's accessible as raw text
            // If it's JSON, you have to parse it.
            alert (jqXhr.responseText);
       }
    }
});
于 2012-10-25T16:43:43.283 に答える
-2

data[0]forループやwhileループなどを介して、データを配列として反復処理する必要があります

于 2012-10-25T16:31:31.713 に答える