-1

正しく動作する AJAX 関数がありますが、ページの後半で関数内で宣言している変数にアクセスできないようです。以下のコードを見ると、機能する関数内で変数 number_rows を警告していることがわかりますが、関数の外でログに記録しようとすると、「未定義」として返されます。

var newData = "user_id=" + id;

    $.ajax({
        type: 'POST', // HTTP method POST or GET
        url: 'ajax.php', //Where to make Ajax calls
        dataType:'text', // Data type, HTML, json etc.
        data:newData, //post variables
        success:function(response){

            var number_rows = response;
           alert(number_rows);

        },
        error:function (xhr, ajaxOptions, thrownError){
            alert(xhr + " " + ajaxOptions + " " + thrownError); //throw any errors
        }
    });

console.log(number_rows);

私の範囲が外れている可能性があることを理解しています。すべてのコードを success 関数内に移動しようとしましたが、他の多くの問題が発生したため、最も簡単な方法は、応答変数をページの残りのコード全体で使用できるグローバル変数にすることです。私も次のようなことを試しました:

success:function(response){

        $('body').append('<span class="ajax_response" id="' + response + '"></span>');

    }

var number_rows = $('.ajax_response').attr('id');
console.log(number_rows);

しかし、何らかの理由で、すぐに ID 値を取得することができません。スパンが正しい ID 値で作成されていることを確認できますが、何らかの理由で正しく処理されません。これについての助けをいただければ幸いです。

ありがとう!

4

3 に答える 3

-2

構成に追加するだけです:

async:false

コードは次のようになります。

var newData = "user_id=" + id;
var number_rows = ""; //This is a global variable

    $.ajax({
        type: 'POST', 
        url: 'ajax.php',
        dataType:'text', 
        data:newData,
        async:false, //<------This is the option you must add
        success:function(response){

            number_rows = response;  //<----- receive data from the server
           alert(number_rows);

        },
        error:function (/*stuff here*/){
            //stuff here
        }
    });

  //Check data
   console.log(number_rows); 

がんばれ^^!

于 2013-09-12T18:54:37.663 に答える
-2

変数のスコープを変更しました。async: false
を 変更するか、サーバーからの応答を取得するまで待ってから、値を使用します。

var newData = "user_id=" + id;
var number_rows = "";  //Declare Here

    $.ajax({
        type: 'POST', // HTTP method POST or GET
        url: 'ajax.php', //Where to make Ajax calls
        dataType:'text', // Data type, HTML, json etc.
        data:newData, //post variables
        async: false,
        success:function(response){

            number_rows = response;  //assign here
           alert(number_rows);

        },
        error:function (xhr, ajaxOptions, thrownError){
            alert(xhr + " " + ajaxOptions + " " + thrownError); //throw any errors
        }
    });

console.log(number_rows);  //Use here
于 2013-09-12T18:43:36.737 に答える
-2

はい、あなたのスコープはオフです。関数内で変数を定義すると、外部からアクセスできなくなります。また、あなたの ajax 呼び出しは非同期であり、任意のコードです。

あなたのconsole.logをsuccess関数の中に入れてください。そうしないと、変数を関数の外でグローバルに宣言しても、おそらく未定義のログが記録されます。

var newData = "user_id=" + id;
var number_rows;
$.ajax({
    type: 'POST', // HTTP method POST or GET
    url: 'ajax.php', //Where to make Ajax calls
    dataType:'text', // Data type, HTML, json etc.
    data:newData, //post variables
    success:function(response){
        number_rows = response;
        alert(number_rows);
        console.log(number_rows); // here, so we are sure it's set. 
        },
        error:function (xhr, ajaxOptions, thrownError){
            alert(xhr + " " + ajaxOptions + " " + thrownError); //throw any errors
        }
    });
于 2013-09-12T18:49:56.333 に答える